Caused By: Android.database.sqlite.sqliteexception: No Such Table: Book (code 1 Sqlite_error)
Solution 1:
What I'm doing in my app is exactly the same thing, creating a "my_db.sql" -> saving it into the "raw" folder and then copying it and creating my DB at runtime.
I had the same issue. The app seemed to work fine on all lower versions of Android except the Pie. After much deliberation and analysis, what worked for me is adding one single line "db.disableWriteAheadLogging();" in the "onOpen()" method.
@OverridepublicvoidonOpen(SQLiteDatabase db) {
super.onOpen(db);
db.disableWriteAheadLogging();
}
Solution 2:
Here's what worked in my case... Made changes to the overall class structure... Below is the edited new class...
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
publicclassBookDatabaseextendsSQLiteOpenHelper {
privatestaticStringDB_NAME="xyz.db";
privatestaticStringDB_PATH="";
privatestaticfinalintDB_VERSION=1;
private SQLiteDatabase mDataBase;
privatefinal Context mContext;
privatebooleanmNeedUpdate=false;
publicBookDatabase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
if (android.os.Build.VERSION.SDK_INT >= 17)
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
elseDB_PATH="/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
copyDataBase();
this.getReadableDatabase();
}
publicvoidupdateDataBase()throws IOException {
if (mNeedUpdate) {
FiledbFile=newFile(DB_PATH + DB_NAME);
if (dbFile.exists())
dbFile.delete();
copyDataBase();
mNeedUpdate = false;
}
}
publicintupdate(String table, ContentValues values, String whereClause, String[] whereArgs){
SQLiteDatabasemDataBase=this.getWritableDatabase();
return mDataBase.update(table, values, whereClause, whereArgs);
}
publiclongcountRows(String query){
SQLiteDatabasedb=this.getWritableDatabase();
return DatabaseUtils.longForQuery(db, query, null);
}
publiclonginsert(String table, ContentValues values){
SQLiteDatabasedb=this.getWritableDatabase();
return db.insert(table, null, values);
}
publiclongdelete(String table, String where, String[] whereArgs){
SQLiteDatabasemDataBase=this.getWritableDatabase();
return mDataBase.delete(table, where, whereArgs);
}
public Cursor rawQuery(String sql,String[] args){
SQLiteDatabasemDataBase=this.getWritableDatabase();
return mDataBase.rawQuery(sql, args);
}
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
SQLiteDatabasedb=this.getWritableDatabase();
return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
privatebooleancheckDataBase() {
FiledbFile=newFile(DB_PATH + DB_NAME);
return dbFile.exists();
}
privatevoidcopyDataBase() {
if (!checkDataBase()) {
this.getReadableDatabase();
this.close();
try {
copyDBFile();
} catch (IOException mIOException) {
thrownewError("ErrorCopyingDataBase");
}
}
}
privatevoidcopyDBFile()throws IOException {
//InputStream mInput = mContext.getAssets().open(DB_NAME);InputStreammInput= mContext.getResources().openRawResource(R.raw.xyz);
OutputStreammOutput=newFileOutputStream(DB_PATH + DB_NAME);
byte[] mBuffer = newbyte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0)
mOutput.write(mBuffer, 0, mLength);
mOutput.flush();
mOutput.close();
mInput.close();
}
publicbooleanopenDataBase()throws SQLException {
mDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
@Overridepublicsynchronizedvoidclose() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
mNeedUpdate = true;
}
}
Solution 3:
Use following method on SQLiteOpenHelper class
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
db.disableWriteAheadLogging();
}
}
Solution 4:
If you are sure about the integrity of the database and the correctness of your code, then the most probable cause of the error could be because your asset file was compressed when the APK was built.
To disable automatic compressing of your database files, include the following code snippet in your app build.gradle file within the android block.
aaptOptions {
noCompress 'db'
}
Hope this solved your problem...
Post a Comment for "Caused By: Android.database.sqlite.sqliteexception: No Such Table: Book (code 1 Sqlite_error)"