I made a database file in python using sqlite3. The database holds 2 tables ANSWERS,QUESTIONS
. I want to use that file in my android app and i followed this post to copy it instead of creating it. I copied the file,implemented the methods as shown,but I get an SQLiteException: no such table: QUESTIONS (code 1 SQLITE_ERROR): , while compiling: select * from QUESTIONS;
. I double and triplechecked my database in sqlite,the tables are there,the names are correct. What could be the issue at place? My helper class looks like this:
public class DatabaseHelper extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
private static String DB_NAME ="quiz.db"; // Database name
private static int DB_VERSION = 1; // Database version
private final File DB_FILE;
private SQLiteDatabase mDataBase;
private final Context mContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
DB_FILE = context.getDatabasePath(DB_NAME);
this.mContext = context;
}
public void createDataBase() throws IOException {
// If the database does not exist, copy it from the assets.
boolean mDataBaseExist = checkDataBase();
if (!mDataBaseExist) {
this.getReadableDatabase();
this.close();
try {
// Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
} catch (IOException mIOException) {
throw new Error("ErrorCopyingDataBase");
}
}
}
// Check that the database file exists in databases folder
private boolean checkDataBase() {
return DB_FILE.exists();
}
// Copy the database from assets
private void copyDataBase() throws IOException {
InputStream mInput = mContext.getAssets().open(DB_NAME);
OutputStream mOutput = new FileOutputStream(DB_FILE);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
// Open the database, so we can query it
public boolean openDataBase() throws SQLException {
mDataBase = SQLiteDatabase.openDatabase(String.valueOf(DB_FILE), null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
@Override
public synchronized void close() {
if(mDataBase != null) {
mDataBase.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {}
public void testQuery() {
mDataBase.rawQuery("select * from QUESTIONS;",null);
}
}
And I call it like this:
helper = new DatabaseHelper(this);
try {
helper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
try{
helper.openDataBase();
}catch (SQLException e){
e.printStackTrace();
}
helper.testQuery();
EDIT:
app/src/main/assets/quiz.db
The path from the.db
file I physically copied to the projectdata/data/com.rocket.src/databases
This path containsquiz.db
quiz.db-journal
andquiz.dbquiz.db