Kevin
Kevin

Reputation: 23634

Android: android.database.sqlite.SQLiteException: no such column

Cursor cursor = db.query(UserTable, null, 
                "UserName=?", new String[] {username}, 
                null, null, AccessedDate);

I am getting this no column found exception when i am trying to open my activity. The error points to the abovequery line.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.text.sample/com.text.sample.usercontext.RecentsActivity}: android.database.sqlite.SQLiteException: no such column: UserName (code 1): , while compiling: SELECT * FROM Patient WHERE UserName=? ORDER BY AccessedDate at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteException: no such column: UserName (code 1): , while compiling: SELECT * FROM Patient WHERE UserName=? ORDER BY AccessedDate at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) at com.text.sample.common.DAL.getAllPatients(DAL.java:104) at com.text.sample.usercontext.RecentsActivity.onCreate(RecentsActivity.java:38) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11 more

Upvotes: 1

Views: 5188

Answers (2)

Tushar Saha
Tushar Saha

Reputation: 2106

I guess naming the column as a constant should help.. this is one of the most common mistakes so instead of remembering name of each column why not place in constants class or columns class as static final member and then access it

It will be some thing like this:

Cursor cursor = db.query(UserTable, null, 
            UserColumn.USER_NAME + "=?", new String[] {username}, 
            null, null, UserColumn.ACCESSED_DATE);

this will give you more control and weed out silly mistakes.

Upvotes: 0

Chor Wai Chun
Chor Wai Chun

Reputation: 3236

A possible cause of "I'm sure the column is there", but "no such column" is returned, would be your database previously has another format being installed, and you updated it without incrementing your db version, and using onUpgrade to update your database.

If this matches your case, kindly have a look at this post, it teaches you how to perform onUpgrade correctly: https://stackoverflow.com/a/14579351/1131470

Upvotes: 1

Related Questions