SQLite table does not exist exception for existing SQLite database (and table)
I've followed the instructions given here for introducing an existing SQLite database to your Android app.
When I query the table "android_metadata" this is fine. But when I run a similar query on my own table "words" (which has _id for primary integer key) I get a table does not exist exception and开发者_JAVA百科 the app crashes.
Why is that?
Code:
Cursor c = myDatabase.query("android_metadata", null, null, null, null, null, null, null);
works but
Cursor c = myDatabase.query("words", null, null, null, null, null, null, null);
returns a table does not exist exception.
This is how I'm creating the database (the references to paths and filenames are correct):
private void copyDatabase() throws IOException
{
//Open local db as the input stream
InputStream myInput = mContext.getAssets().open(DB_NAME);
//Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length = 0;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
(Note: To my eyes, the table is there. I'm looking right at it in my SQLite browser.)
You're following a red herring. The android_metadata table is created in every android database no matter what.
The real mechanism for determining your issue is to simply run your app in the emulator and check out the database. If your emulator is running and the application has setup the database, run:
adb shell
cd /data/data/your.application.package/databases
ls
if ls
shows the database name you expect, run:
sqlite3 <your db file>
at the sqlite3 prompt run : .schema
This will print out the tables of the databases. My guess is that you'll find just the meta data table because android is not actually reading your database from your external location. If thats the case, comment back and I can try to help you through that process.
You could try to create your DB instead with the SQLiteHelpter. It checks if one exists and if not it creates one, without any extra effort for you.
I was already posting an example on other question, check it here:
Android - Sqlite database method undefined fot type
Hope this helps :)
精彩评论