Inserting values to SQLite table in Android

前端 未结 8 2063
抹茶落季
抹茶落季 2020-12-09 16:42

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

public class cashbook extends Activity {

    @Over         


        
相关标签:
8条回答
  • 2020-12-09 16:58

    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);
    
    0 讨论(0)
  • 2020-12-09 16:58
    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

    0 讨论(0)
  • 2020-12-09 16:59

    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());
     }
    
    0 讨论(0)
  • 2020-12-09 17:05

    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.

    0 讨论(0)
  • 2020-12-09 17:07

    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

    0 讨论(0)
  • 2020-12-09 17:17

    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;
    }
    
    0 讨论(0)
提交回复
热议问题