Query for sqlite in android (insert request)

I want to insert into the database .. form value. Two of them are integers, and one is a string .. but I cannot view the value that I entered in my sqlite viewer for the database. I am using the following code:

SQLiteDatabase myDB= null;
      String TableName = "basicdetai";
     try{ 

          myDB = this.openOrCreateDatabase("Medical", MODE_PRIVATE, null); 

          /* Create a Table in the Database. */
         myDB.execSQL("CREATE TABLE IF NOT EXISTS "
            + TableName
            + " (order_id INT(4), doc_id INT(4),doc_name VARCHAR );");
         /* Insert data to a Table*/
          myDB.execSQL("INSERT INTO "
            + TableName
            + "(order_id,doc_id,doc_name)"
           + " VALUES ("+ ordid + ","+ doci + ","+ docnam +");");
        // line 10  + " VALUES ("+ a + ","+ b + ","+ docnam +");");
        //  line 11  +"VALUES (5011,4011,'gupta');");



      }
      catch(Exception e) {
          Log.e("Error", "Error", e);
         } 
      finally {
           if (myDB != null)
            myDB.close();
          }

but when I use line 11 in the above code, I can see the record (5011,4011,'gupta')through sqlite browser. I perform the following operation to convert a string value that I get from a form to an integer

try {
          ordid = Integer.parseInt(orderid.getText().toString());
          doci = Integer.parseInt(docid.getText().toString());
         // qn = Integer.parseInt(qty.getText().toString());
      } catch(NumberFormatException nfe) {
         System.out.println("Could not parse " + nfe);
      }

Help Plz.

+5
source share
3 answers

Please try to complete the request ...

db.execSQL("insert into your table name (order_id,doc_id,doc_name)" + "values("ordid+","+doci+","
                + "\"" + docnam + "\"" + ") ;");
+8
source

Android , INSERT. ?

:

    public void addLibrary(LibraryItem item) {
    ContentValues row = new ContentValues();
    row.put("title", item.getTitle());
    row.put("author", item.getAuthor());
    row.put("publisher", item.getPublisher());
    row.put("thumbnailUrl", item.getThumbnail());
    row.put("formatType", item.getFormat());
    row.put("contentUrl", item.getContentUrl());
    row.put("publicationType", item.getPublicationType());
    row.put("favorite", item.isFavorite() ? "YES" : "NO");
    row.put("id", item.getItemId());
    row.put("normalizedTitle", StringUtil.normalize(item.getTitle()));
    row.put("downloaded", item.hasContent() ? Calendar.getInstance()
            .getTimeInMillis() : 0);
    row.put("wasdownloaded", item.hasContent() ? "YES" : "NO");
    row.put("bookmarked", "NO");
    row.put("archived", "NO");

    long id = db.insert("LIBRARY", null, row);
    }
+10
Simple and easy to understand In this code i am using two fields.
//Activity.java

package com.virtual.university;

import java.util.Locale;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class VirtualActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
     private String tablename="StudentInfo";
     EditText t1,t2;
     Button b1,b2;
     SQLiteDatabase db;
     public static final String CONTENT1 = "sid";
     public static final String CONTENT2 = "pasw";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        t1=(EditText)findViewById(R.id.editText1);
        t2=(EditText)findViewById(R.id.editText2);
        b1=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2=(Button)findViewById(R.id.button1);
        b2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                t1.setText("");
                t2.setText("");
            }
        });
        // create or open database file
        db = openOrCreateDatabase("Login.db" , SQLiteDatabase.CREATE_IF_NECESSARY,null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);

        //Create table
        db.execSQL("CREATE TABLE IF NOT EXISTS "+tablename+" " +
                "( "+CONTENT1+" INTEGER PRIMARY KEY AUTOINCREMENT," +
                "  "+CONTENT2+" TEXT ); ");
       //TextView txtView=(TextView)findViewById(R.id.txtView);
       //txtView.setText("Table created");
    }
    public void onClick(View v) 
    {
    insert();   
    }
    private void insert() {

        int uid=Integer.parseInt(t1.getText().toString());
        String pwd=t2.getText().toString();
        String sql1 = "insert into " +tablename+ " (" +CONTENT1+ ", " +CONTENT2+ ") values(" +uid+  ",'" +pwd+ "')";
        db.execSQL(sql1);
        Toast.makeText(getBaseContext(),"inserted", Toast.LENGTH_SHORT).show(); 
    }}
+2
source

All Articles