SQLite3,找不到表

时间:2011-12-20 00:57:41

标签: android database sqlite

我有几个月的时间开发Android作为hobbie,这是我第一次尝试使用数据库。我把这个类作为数据库助手:

public class ContactsDbAdapter {
    public static final String KEY_NAME = "name";
    public static final String KEY_CONTACT_ID = "contactID";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "ContactsDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement, "
            + "name text not null, contactID text not null);";
    private final Context mCtx;
    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "contacts";
    private static final int DATABASE_VERSION = 2;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            Log.v("t", "super(context,DATABSENAME,null..etc)");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
            Log.v("t", "db.execSQL(database_create)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            //TODO
        }
    }

    public ContactsDbAdapter(Context ctx) {
        this.mCtx = ctx;
        Log.v("t", "Constructor, this.mCtx=ctx");
    }

    public ContactsDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public long addContact(String name, String contactId) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_CONTACT_ID, contactId);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

}

并在主要的Activity onCreate()方法上用

初始化数据库
        mDbHelper = new ContactsDbAdapter(this);
        mDbHelper.open();

然后在我的代码的某处我调用数据库

mDbHelper.addContact(name, contactId);

失败,因为数据库显然找不到名为contacts的表:

12-19 19:48:50.436: I/Database(276): sqlite returned: error code = 1, msg = no such table: contacts

任何想法我做错了,也许我在创建数据库时做错了。

谢谢!

0 个答案:

没有答案