Android and SQlite - missing a column?
Gabriu
Here is my DataBase.java. However, when queried in MainActivity, LogCat shows "No such column: Event_id"
static final String tableName="EventList";
static final String colID="Event_id";
static final String colTitle="Title";
static final String colDetails="Details";
static final String colYear="YY";
static final String colMonth="MM";
static final String colDay="DD";
static final String colHour="HH";
static final String colMinute="TT";
public DateBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createDB= "create table "+tableName+"("+colID+"integer primary key , "+colTitle+" text not null, "+colDetails+" text, "+colYear+" int, "+colMonth+" int, "+colDay+" int, "+colHour+" int, "+colMinute+" int)";
db.execSQL(createDB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableSQL = "DROP TABLE IF EXISTS " + tableName + " ";
db.execSQL(dropTableSQL);
onCreate(db);
}
public void execSQL(String sql, Object[] args) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, args);
}
public Cursor query(String sql, String[] args) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, args);
return cursor;
}
}
LogCat reports:
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gabelyu.myapplication/com.example.gabelyu.myapplication.MainActivity}: android.database.sqlite.SQLiteException: no such column: Event_id (code 1): , while compiling: SELECT Event_id as _id, Title, Details, YY, MM, DD, HH, TT FROM EventList
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:154)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: no such column: Event_id (code 1): , while compiling: SELECT Event_id as _id, Title, Details, YY, MM, DD, HH, TT FROM EventList
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1090)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:663)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1267)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1138)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1306)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.example.gabelyu.myapplication.MainActivity.onCreate(MainActivity.java:40)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5255)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.qihoo360.mobilesafe.loader.c.callActivityOnCreate(SourceFile:98)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:154)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
Does this mean that the primary key cannot be selected? Or does android just supervise the column? thanks.
Julian
You forgot to put the correct spaces somewhere, that's why it created the wrong table. Here is the correct string code:
String createDB = "create table "+tableName+" ("+colID+" integer primary key , "+colTitle+" text not null, "+colDetails+" text, "+colYear+" int, "+colMonth+" int, "+colDay+" int, "+colHour+" int, "+colMinute+" int)";
Edit: I formatted your sql query string, let me know if it works. Can you also edit the post and include the sql command to get the item from the database?
String createDB = "CREATE TABLE " + tableName + "(" + colID + " INTEGER PRIMARY KEY," + colTitle + " TEXT not null," + colDetails + " TEXT," + colYear + " INTEGER," + colMonth + " INTEGER," + colDay + " INTEGER," + colHour + " INTEGER," + colMinute + " INTEGER)";