-1

I have created a database with 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?

MPhil
  • 881
  • 1
  • 12
  • 24
Suprad Parashar
  • 39
  • 2
  • 11
  • what is the value of `NoteEntry.TABLE_NAME_NOTES` – akhilesh0707 Jul 25 '17 at 09:32
  • @AkhileshPatil, NoteEntry.TABLE_NAME_NOTES = "notes" – Suprad Parashar Jul 25 '17 at 09:32
  • Have you called onCreate of your DBHelper class anywhere to create your table in the DB? – Kapil G Jul 25 '17 at 09:36
  • @kapsym, I didn't quite get you, but, I have called the constructor of the DbHelper class in the displayDatabaseInfo() method in NoteActivity.java. – Suprad Parashar Jul 25 '17 at 09:39
  • Yeah but that only creates an empty database for you with the name that you gave. After that you need to create the table as well inside that database. For that you need to call the oncreate method manually or you can implement the onUpgrade method and inside it call the onCreate which will run automatically when your app is installed. – Kapil G Jul 25 '17 at 09:40

1 Answers1

1

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);
}
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51