Suprad Parashar Suprad Parashar - 3 years ago 112
Android Question

Table Does Not Exist Error Android

I have created a Database and also a table in it. When I try to extract the data present in the table, Android Studio shows an error saying that the table does not exist.

I referred to these links for help


  1. SQL table not found exception

  2. http://mobisys.in/blog/2012/01/tutorial-using-database-in-android-applications/



The First Link did not answer my question and the second link was way too complicated.

Here is the extract of my code.

DbHelper.java:

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase database) {

String CREATE_TABLE_NOTES_QUERY = "CREATE TABLE " +
NoteEntry.TABLE_NAME_NOTES + " (" +
NoteEntry._ID_NOTES + " INTEGER NOT NULL AUTOINCREMENT, " +
NoteEntry.COLUMN_COUNTRY + " TEXT NOT NULL, " +
NoteEntry.COLUMN_DENOMINATION + " REAL NOT NULL DEFAULT 0, " +
NoteEntry.COLUMN_YEAR + " INTEGER, " +
NoteEntry.COLUMN_OBVERSE_DESCRIPTION + " TEXT DEFAULT \"Not Available\", " +
NoteEntry.COLUMN_REVERSE_DESCRIPTION + " TEXT DEFAULT \"Not Available\", " +
NoteEntry.COLUMN_LENGTH + " REAL DEFAULT 0.0, " +
NoteEntry.COLUMN_BREADTH + " REAL DEFAULT 0.0, " +
NoteEntry.COLUMN_THICKNESS + " REAL DEFAULT 0.0);";

database.execSQL(CREATE_TABLE_NOTES_QUERY);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


NoteActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NoteActivity.this, NoteEditorActivity.class);
startActivity(intent);
}
});

displayDatabaseInfo();

}

private void displayDatabaseInfo() {

CollectoDbHelper mDbHelper = new CollectoDbHelper(this);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + CollectoContract.NoteEntry.TABLE_NAME_NOTES, null);
try {
TextView displayView = (TextView) findViewById(R.id.textview_note);
displayView.setText("Number of rows in notes table: " + cursor.getCount());
} finally {
cursor.close();
}
}


LogCat:

07-25 14:55:57.357 9162-9162/com.example.android.collecto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.collecto, PID: 9162
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.collecto/com.example.android.collecto.NoteActivity}: android.database.sqlite.SQLiteException: no such table: notes (code 1): , while compiling: SELECT * FROM notes
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.sqlite.SQLiteException: no such table: notes (code 1): , while compiling: SELECT * FROM notes
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
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.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1284)
at com.example.android.collecto.NoteActivity.displayDatabaseInfo(NoteActivity.java:49)
at com.example.android.collecto.NoteActivity.onCreate(NoteActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6743)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6334) 
at java.lang.reflect.Method.invoke(Native Method)


Can Somebody Guide me to solve my error?

Answer Source

Try to call onUpgrade() and call onCreate() inside;

Also please uninstall app and reinstall

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_NOTES_QUERY);
    // Create tables again
    onCreate(db);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download