开发者

onCreate not being called after getWritableDatabase/getReadableDatabase

My app's got a database with three tables in it: one to store the names of the people it tracks, one to track an ongoing event, and one - for lack of a better term - for settings.

I load the first table when the app starts. I ask for a readable database to load in members to display, and later I write to the database when the list changes. I've had no problems here.

The other two tables, however, I can't get to work. The code in the helper classes is identical with the exception of class names and column names, and (at least until the point where I try to access the table) the code to use the table is nearly identical as well.

Here's the code for my helper class (I've got a separate helper for each table, and as I said, it's identical except for class names and columns):

public class db_MembersOpenHelper extends SQLiteOpenHelper
{
    public static final String TABLE_NAME = "members_table";
    public static final String[] COLUMN_NAMES = new String[] {
            Constants.KEY_ID,
            "name",
            "score"
    };
    private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " ("
            + COLUMN_NAMES[0] + " INTEGER PRIMARY KEY autoincrement, "
            + COLUMN_NAMES[1] + " TEXT, "
            + COLUMN_NAMES[2] + " INTEGER);";

开发者_运维知识库    public db_MembersOpenHelper(Context context)
    {
        super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_CREATE); }

    @Override
    public void  onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion + ".");
        // Do nothing. We do not have any updated DB version
    }
}

Here's how I use it successfully:

db_MembersOpenHelper membersDbHelper = new db_MembersOpenHelper(this);
SQLiteDatabase membersDb = membersDbHelper.getReadableDatabase();
Cursor membersResult = membersDb.query(TABLE_NAME, null, null, null, null, null, null);
members = new HashMap<String, Integer>();

membersResult.moveToFirst();
for(int r = 0; r < membersResult.getCount(); r++)
{
    members.put(membersResult.getString(1), membersResult.getInt(2));
    membersResult.moveToNext();
}
membersDb.close();

And here's where it fails:

db_PlayersOpenHelper playersDbHelper = new db_PlayersOpenHelper(this);
final SQLiteDatabase playersDb = playersDbHelper.getWritableDatabase();
if(newGame)
{
    for(String name : players)
    {
        ContentValues row = new ContentValues();
        row.put(COLUMN_NAMES[1], name);
        row.put(COLUMN_NAMES[2], (Integer)null);
        playersDb.insert(TABLE_NAME, null, row);
    }
}

The first one works like a charm. The second results in ERROR/Database(6739): Error inserting achievement_id=null name=c android.database.sqlite.SQLiteException: no such table: players_table: , while compiling: INSERT INTO players_table(achievement_id, name) VALUES(?, ?); ...

I did do some testing, and the onCreate method is not being called at all for the tables that aren't working. Which would explain why my phone thinks the table doesn't exist, but I don't know why the method isn't getting called.

I can't figure this out; what am I doing so wrong with the one table that I accidentally did right with the other?


I think the problem is that you are managing three tables with with three helpers, but only using one database. SQLiteOpenHelper manages on database, not one table. For example, it checks to see whether the database, not table, exists when it starts. It already does, so onCreate() does not fire.

I would manage all tables with one helper.


Let me see if I get this right. You are trying to create one database with three tables. But when you create the database, you create just one table; you are somehow instantiating the same database at a different place and wonder why its onCreate method doesn't get called. Is this a correct interpretation?

My strategy would be to try and create all three tables in the single onCreate() method.


If you are working with multiple tables, then you have to create all of the tables at once. If you have run your application first and later you update your database, then it will not upgrade your DB.

Now delete your application, then run it again.


There is one more solution but it is not proper. You can declare onOpen method in which you can call onCreate. And add IF NOT EXISTS before table name in your create table string. – Sourabh just now edit

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜