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.

8 Answers 8

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);
Sign up to request clarification or add additional context in comments.

6 Comments

It shows error on emulator - The application CashBook has stopped unexpectedly.-Force Close
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.
How can i debug? i put some break points ..but thats not working.Which is the steps for debug?
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.
Deserve more like. This is the appropriate way to do it
|
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

5 Comments

Here we are passing it as string, how can i pass the current date value by this query.
use the systeminbuilt function for getting current date :)
i tried by this methode : INSERT INTO CashData (Description,Amount,Trans) VALUES("+a+","+b+","+Trans+") But error on inserting shows : no such column like "value of a"
db.execSQL("INSERT INTO " + TABLENAME + " VALUES (" + t + ");"); here t is is your variable
This is begging for an eventual SQL injection issue. Use db.insert() instead.
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());
 }

Comments

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

Comments

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

Comments

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

3 Comments

ok you have tried and removed id values from insert statment?
you very first just create a table whether it creates or not see then try to insert values into it
table is already created but the values not inserting,Id removed ,still same pblm
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

Comments

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.

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.