Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No such table SQLite Android

Tags:

android

sqlite

I have built a database helper class with an open() method and extended sqlite helper with onCreate() overridden. (shown below). Despite all of this, I am getting 'SQLiteException, no such table' error. I do not understand, why is the openHelper not helping?

public void open() {
    try{
        db = openHelper.getWritableDatabase();
    } catch (SQLiteException e) {
        db = openHelper.getReadableDatabase();
    }
}

//other stuff

public static final String database_create = "create table " + database_table + " (" + primary_key + " integer primary key autoincrement, " 
    + company_column + " text not null, " + product_column + " text not null);";

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(database_create);
    }

the following code is meant to insert an entry temporarily, because the database cannot be empty for other reasons. It seems to execute perfectly, yet the last bit of code, which comes after is what throws the error

CompanyAndProductDatabaseAdapter cpdAdapter = new CompanyAndProductDatabaseAdapter(this);
    cpdAdapter.open();
    errorguard = cpdAdapter.insertPair("Loading", "...");
    cpdAdapter.close();

//other stuff

cpdAdapter.open();
    Cursor cursor = cpdAdapter.getAllPairsCursor(); //error here
    cursor.requery();
    startManagingCursor(cursor);
like image 653
reader1 Avatar asked Dec 09 '25 12:12

reader1


2 Answers

I don't know why you implemented a open-method, also the database_create is not what it should be.
I assume the first code is part of CompanyAndProductDatabaseAdapter.

Take a look here:
Android - Sqlite database method undefined fot type

That's almost all you need to create/get a DB with inherted SQLiteOpenHelper.

like image 106
Beasly Avatar answered Dec 11 '25 02:12

Beasly


Your problem is this function:

    db = openHelper.getWritableDatabase();
    db = openHelper.getReadableDatabase();

First: check your path/name of the database is correct. It can create a default database, an empty database ( no tables, no nothing) if the database is not found.

Second: try to open your database this way:

String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); // or OPEN_READONLY, depending on situation.
like image 38
danigonlinea Avatar answered Dec 11 '25 02:12

danigonlinea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!