Unable to read SQLite table from Android
I am trying to open a SQLite database in android, the database works fine in the sqlite browser, but android does not read that the table in question exists. Below is the code I'm tinkering with.
public String setColorName() {
String colorName = null;
try {
db = this.openOrCreateDatabase("colors.db",MODE_WORLD_READABLE, null);
Cursor cursor = db.rawQuery("SELECT colorName FROM Colors WHERE RGB = '000000'", null);
if (cursor != null) {
if (cursor.moveToFirst()){
do {
colorName = cursor.getString(cursor.getColumnIndex("colorName"));
}while (cursor.moveToNext());
}
} else {
colorName = "woops";
}
} catch (NumberFormatException e) {}
return colorName ;
}
I have found many articles regarding this issue on several forums (including stackoverflow) but the issue I'm having didn't seem to get resolved with their solutions. I'm trying to pull the RGB and colorname from the code (the query above is just a test leading up to that) so that I can estimate the nearest RGB using the euclidean distance formula on each portion. In order to do that, I need to be able to atleast grab the data from the table :P.
LogCat:
I/Database(12748): sqlite returned: error code = 1, msg = no such table: Colors
D/AndroidRuntime(12748): Shutting down VM
W/dalvikvm(12748): threadid=1: thread exiting with uncaught exception (group=0x4
0015560)
E/AndroidRuntime(12748): FATAL EXCEPTION: main
E/AndroidRuntime(12748): android.database.sqlite.SQLiteException: no such table:
Colors: , while compiling: SELECT colorName FROM Colors WHERE RGB = '000000'
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteCompiledSql.nat
ive_compile(Native Method)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteCo开发者_Go百科mpiledSql.com
pile(SQLiteCompiledSql.java:92)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteCompiledSql.<in
it>(SQLiteCompiledSql.java:65)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteProgram.<init>(
SQLiteProgram.java:83)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteQuery.<init>(SQ
LiteQuery.java:49)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteDirectCursorDri
ver.query(SQLiteDirectCursorDriver.java:42)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteDatabase.rawQue
ryWithFactory(SQLiteDatabase.java:1356)
E/AndroidRuntime(12748): at android.database.sqlite.SQLiteDatabase.rawQue
ry(SQLiteDatabase.java:1324)
And finally, here's a picture of the SQLite browser showing that the query does work on that database SQLiteBrowser Query
Incase the picture didn't give it away, I'm storing the colors.db in assets.
Sorry for the length, I try to be as thorough as possible and seriously appreciate anyone who helps me.
Ok, it looks like I got past the issue. I needed to copy the db from assets and then open it from there. I ended up making a database helper using the information I found in the link below and I got the database to stop returning that error, though I now need to complete the rest of the application. Thanks a ton for all the great advise, everyone! http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
From your screenshot it appears you have the colors.db in your assets directory.
While I am not sure exactly how to load a SQLite db from there, I do know that by default the SQLiteDatabase
android helper object reads and writes to the protected data folder of the app. So your openOrCreateDatabase
method is invoking a create. But it bombs on the query because the table doesnt exist in this freshly minted db.
I'm sure someone else or a little googling can help resolve this further.
You are creating the database and after that you wish to access the table from it. Don't you need to create the tables too? I'd suggest you to use SQLiteOpenHelper instead of openOrCreateDatabase
so you won't run into this problem.
If you wanna access an existing database through assets
(as I've seen on your screenshot), you could refer to SQLiteException: no such table
精彩评论