Reputation: 7411
I have a table with 2 columns.
I am trying to insert a value into this table with the help of SqliteOpenHelper object. But it throws an exception:
public void logTime() {
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_LOG_TIME, null, new ContentValues());
}
Exception:
E/SQLiteLog: (1) near "null": syntax error
E/SQLiteDatabase: Error inserting
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO log_time(null) VALUES (NULL)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.livinglifetechway.mytime.db.DatabaseHelper.logScreenUnlock(DatabaseHelper.java:62)
at com.livinglifetechway.mytime.ScreenUnlockBroadcastReceiver.onReceive(ScreenUnlockBroadcastReceiver.java:25)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
at android.app.ActivityThread.access$1800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1428)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Database onCreate call:
@Override
public void onCreate(SQLiteDatabase db) {
// Create Statements
String CREATE_TABLE_LOG_TIME = "CREATE TABLE " + TABLE_LOG_TIME + " " +
"(" +
KEY_LOG_TIME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_LOG_TIME_TIME + " DATETIME DEFAULT CURRENT_TIMESTAMP" +
")";
// Execute statements
db.execSQL(CREATE_TABLE_LOG_TIME);
}
All values are default/auto_increment values.
How can I insert a record in this table?
Upvotes: 0
Views: 833
Reputation: 1422
replace this
db.insert(TABLE_LOG_TIME, null, new ContentValues());
with
db.insert(TABLE_LOG_TIME, null, null);
Upvotes: 0
Reputation: 152817
To insert a row with default values, specify at least one column with null value:
ContentValues cv = new ContentValues();
cv.putNull(KEY_LOG_TIME_TIME);
db.insert(TABLE_LOG_TIME, null, cv);
Upvotes: 1