0

I'm trying to develop my app so that it uses a database to store data about books. My knowledge of android and SQLite is scanty at best, so my code may look pretty awkward. When I try running the app, an error occurs stating that

Caused by: android.database.sqlite.SQLiteException: no such table: Library_Table (code 1): , while compiling: SELECT _id, Book_Title, Author, Length, Chapters_Read, Date_Added FROM Library_Table ORDER BY Date_Added DESC

I believe the problem arises when I call a command to query the database; I'm certain there's something I'm not doing right, but after trying to debug for weeks, I'm about to give up. I'll add the few segments of the code which I think are important to the problem.

I believe this first part creates the table(It occurs in the class which extends the sqliteopenhelper):

public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String CREATE_LIBRARY_TABLE = "CREATE TABLE " + Constants.TABLE_NAME
        + "("
        + Constants.KEY_ID + " INTEGER PRIMARY KEY,"
        + Constants.TITLE_NAME + " TEXT,"
        + Constants.BOOK_AUTHOR + " TEXT,"
        + Constants.BOOK_LENGTH + " INTEGER,"
        + Constants.CHAPTERS_READ + " INTEGER,"
        + Constants.DATE_ADDED + " TEXT"
        + ");";
    sqLiteDatabase.execSQL(CREATE_LIBRARY_TABLE);
}

This next part also occurs the class that extends SQLitesOpenHelper (The line Cursor cursor = database.query; is highlited by android studio as problematic):

public ArrayList<Book> getBooks(){
    String selectQuery = "SELECT * FROM " + Constants.TABLE_NAME;
    SQLiteDatabase database = this.getReadableDatabase();

    Cursor cursor = database.query(
            Constants.TABLE_NAME,
            new String[]{
            Constants.KEY_ID,
            Constants.TITLE_NAME,
            Constants.BOOK_AUTHOR,
            Constants.BOOK_LENGTH,
            Constants.CHAPTERS_READ,
            Constants.DATE_ADDED},
            null, null, null, null,
            Constants.DATE_ADDED + " DESC");

This is the Constants class (in a separate file):

public class Constants {
    public static final String DATABASE_NAME = "LibraryDB";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "Library_Table";
    public static final String KEY_ID = "_id";
    public static final String TITLE_NAME = "Book_Title";
    public static final String BOOK_AUTHOR = "Author";
    public static final String BOOK_LENGTH = "Length";
    public final static String CHAPTERS_READ = "Chapters_Read";
    public static final String DATE_ADDED = "Date_Added";
}

I think the real problem is that I don't know my way around this subject particularly well; I suspect it's not a very big issue to solve.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
David
  • 487
  • 1
  • 5
  • 18

1 Answers1

0

If your app installed on the device already has a SQLite database setup, SQLiteOpenHelper#onCreate is not called.

This is an issue that is usually solved using database migrations from one version of your app to another.

If you're not updating a previous version of the app uninstall the current version and then push your app to the phone again. If you are updating a previous version, you will need to migrate your database using SQLiteOpenHelper#onUpgrade.

More info here.

Community
  • 1
  • 1
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85