reader1
reader1

Reputation: 93

No such table SQLite Android

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);

Upvotes: 3

Views: 4449

Answers (2)

danigonlinea
danigonlinea

Reputation: 1113

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.

Upvotes: 1

Beasly
Beasly

Reputation: 1537

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.

Upvotes: 2

Related Questions