I am using SQLite Database in my application for normal insertion and fetching purpose. I am facing No such table issue while trying to get table row count for the particular table.
Logcat Error:
Fatal Exception: android.database.sqlite.SQLiteException: no such table: table_name(code 1): , while compiling: SELECT * FROM table_name
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.takeoffandroid.database.DBAccess.getCount(SourceFile:384)
at com.takeoffandroid.database.DBAccess.isTableCountLogic(SourceFile:402)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
SQLite create Table query:
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String INTEGER_PRIMARY_KEY = " INTEGER PRIMARY KEY AUTOINCREMENT";
public static final String TABLE_NAME = "table_name";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_URL = "url";
private static final String COLUMN_JSON = "json";
private static final String CREATE_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
COLUMN_ID + INTEGER_PRIMARY_KEY + COMMA_SEP +
COLUMN_URL + TEXT_TYPE + COMMA_SEP +
COLUMN_JSON + TEXT_TYPE + ")";
public static DBAccess init(Context context) {
if (mInstance == null)
synchronized (DBAccess.class) {
if (mInstance == null) {
mInstance = new DBAccess(context);
}
}
return mInstance;
}
public DBAccess(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_QUERY);
}
Method to get table row count:
public int getCount() {
int count=0;
SQLiteDatabase db=getReadableDatabase();
Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, null);
count = cursor.getCount();
CLog.i("Table count", "Table count size is "+String.valueOf(count));
cursor.close();
db.close();
close();
return count;
}
Please help me if there is any issue in my code, I am completely stucK up with this issue since long time and I couldn't find the solution. Any kinda suggestions would be helpful to me.
Note: This is a very random issue which is happening only with few devices.