21

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.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Sinoy Devassy
  • 564
  • 4
  • 11
  • 27

8 Answers8

56

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);
Jon
  • 2,502
  • 4
  • 21
  • 23
  • It shows error on emulator - The application CashBook has stopped unexpectedly.-Force Close – Sinoy Devassy Jun 06 '11 at 11:57
  • Can you debug the error? It should have the specific line number in the adb logcat and that way you can pinpoint which function crashed it. – Jon Jun 06 '11 at 12:52
  • How can i debug? i put some break points ..but thats not working.Which is the steps for debug? – Sinoy Devassy Jun 07 '11 at 04:33
  • 3
    Every Android developer should know how to use the adb logcat tool. Open a terminal or command prompt and navigate to the place where you put/installed the android SDK folder, then go into either tools, platform-tool, or platform-tools (the folder name has changed over time). In one of those folders, you should find the "adb" binary. In the same folder as the adb binary, run the command `adb logcat`. – Jon Jun 07 '11 at 04:52
  • I'm assuming you are working with Eclipse? If so, are you asking how to set breakpoints in Eclipse or how to work with the breakpoints in Eclipse utilizing `adb logcat`? – Jon Jun 07 '11 at 05:28
  • 1
    Deserve more like. This is the appropriate way to do it – Orelsanpls Jul 09 '14 at 11:28
18

okk this is fully working code edit it as per your requirement

public class TestProjectActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SQLiteDatabase db;
    db = openOrCreateDatabase( "Temp.db"        , SQLiteDatabase.CREATE_IF_NECESSARY        , null          );
    try {
        final String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS tbl_Contain ("
                + "ID INTEGER primary key AUTOINCREMENT,"
                + "DESCRIPTION TEXT,"
                + "expirydate DATETIME,"
                + "AMOUNT TEXT,"
                + "TRNS TEXT," + "isdefault TEXT);";
        db.execSQL(CREATE_TABLE_CONTAIN);
        Toast.makeText(TestProjectActivity.this, "table created ", Toast.LENGTH_LONG).show();
        String sql =
            "INSERT or replace INTO tbl_Contain (DESCRIPTION, expirydate, AMOUNT, TRNS,isdefault) VALUES('this is','03/04/2005','5000','tran','y')" ;       
                db.execSQL(sql);
    }
    catch (Exception e) {
        Toast.makeText(TestProjectActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();  
}}}

Hope this is useful for you..
do not use TEXT for date field may be that was casing problem still getting problem let me know :)Pragna

Android
  • 8,995
  • 9
  • 67
  • 108
3

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());
 }
HaemEternal
  • 2,229
  • 6
  • 31
  • 50
2

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;
}
Kaan
  • 379
  • 3
  • 7
1
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

Android
  • 8,995
  • 9
  • 67
  • 108
0

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

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

Android
  • 8,995
  • 9
  • 67
  • 108
0

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

Dimse
  • 1,476
  • 1
  • 10
  • 15