3

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);
reader1
  • 93
  • 1
  • 8

2 Answers2

2

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.

Community
  • 1
  • 1
Beasly
  • 1,517
  • 4
  • 20
  • 30
  • Thanks for the help. The issue is that database open helper doesnt seem to take kindly to creating two tables in the same database. Simply specifying a new database solved everything. How is my create statement incorrect though? It seems to work now, but if it can be made more efficient I'll like that. – reader1 Feb 15 '11 at 02:49
  • Hey, I hope you don't mind if I say take a look at the link. I'm discribing everything there. Tell me if something isn't clear. http://stackoverflow.com/questions/4921550/where-to-close-the-database/4921720#4921720 – Beasly Feb 15 '11 at 07:46
1

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.
Perception
  • 79,279
  • 19
  • 185
  • 195
danigonlinea
  • 1,113
  • 1
  • 14
  • 20