[android] Inserting values to SQLite table in Android

I am new in android app developement. I tried to insert values to SQLite database through the below code;

public class cashbook extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SQLiteDatabase db;

        db = openOrCreateDatabase(
            "cashbookdata.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );

        final String Create_CashBook =
            "CREATE TABLE CashData ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "Description TEXT," 
            + "Amount REAL,"
            + "Trans INTEGER," 
            + "EntryDate TEXT);";

        db.execSQL(Create_CashBook);  
        final String Insert_Data="INSERT INTO CashData VALUES(2,'Electricity',500,1,'04/06/2011')";
        db.execSQL(Insert_Data);

It shows error on emulator - The application CashBook has stopped unexpectedly.

The database and table created , but the value insertion is not working. Please help me to resolve this issue. Thanks.

This question is related to android sqlite

The answer is


I recommend to create a method just for inserting and than use ContentValues. For further info https://www.tutorialspoint.com/android/android_sqlite_database.htm

public boolean insertToTable(String DESCRIPTION, String AMOUNT, String TRNS){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put("this is",DESCRIPTION);
    contentValues.put("5000",AMOUNT);
    contentValues.put("TRAN",TRNS);
    db.insert("Your table name",null,contentValues);

    return true;
}

Since you are new to Android development you may not know about Content Providers, which are database abstractions. They may not be the right thing for your project, but you should check them out: http://developer.android.com/guide/topics/providers/content-providers.html


I see it is an old thread but I had the same error.

I found the explanation here: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

void execSQL(String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

void execSQL(String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.


Seems odd to be inserting a value into an automatically incrementing field.

Also, have you tried the insert() method instead of execSQL?

ContentValues insertValues = new ContentValues();
insertValues.put("Description", "Electricity");
insertValues.put("Amount", 500);
insertValues.put("Trans", 1);
insertValues.put("EntryDate", "04/06/2011");
db.insert("CashData", null, insertValues);

public class TestingData extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SQLiteDatabase db;
    db = openOrCreateDatabase(
        "TestingData.db"
        , SQLiteDatabase.CREATE_IF_NECESSARY
        , null
        );
 }

}

then see this link link


You'll find debugging errors like this a lot easier if you catch any errors thrown from the execSQL call. eg:

 try 
 {
      db.execSQL(Create_CashBook);  
 }
 catch (Exception e) 
 {
       Log.e("ERROR", e.toString());
 }

okkk you have take id INTEGER PRIMARY KEY AUTOINCREMENT and still u r passing value... that is the problem :) for more detail see this still getting problem then post code and logcat