6

I want to insert into a database ..a form's value ..two of them are integer and one is string..but i am not able to view in my sqlite database viewer the value which i am entering.. 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 am using line 11 in above code i can see the record (5011,4011,'gupta') through sqlite browser. I am performing following operation to convert the string value which i am getting from form to 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);
      }

Plz help ..

1
  • What is the exact question/problem? Also, it would be a lot easier to read if you could properly format your code. Commented Apr 19, 2011 at 15:18

3 Answers 3

13

Please try below query...

db.execSQL("insert into your table name (order_id,doc_id,doc_name)" + "values("ordid+","+doci+","
                + "\"" + docnam + "\"" + ") ;");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanx chirag..so strings need to be inserted using double quotes..Thanx a lot.:):)
chirag ..it would be a gr8 help if u can suggest some book or website ..as my work involves lots of query writing..and i am doing my first project in android..and can this table be accessible from different activity???
for that you need to make one database class and write all the queries into it and you can access it from any where..
Actually i want to insert value in database on button click..and i have three buttons ..all performing different tasks...one of them inserting value in db..so i am using switch case to perform tasks..no how will i do the above task by creating seperate database class..is there any way?
Use bind parameters for security reasons
11

Android has some help classes that help on INSERT. Why don't you use them?

Example:

    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);
    }

Comments

3
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(); 
    }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.