我有一个数据库已经包含一个表“LocalLogin”并且已经正确设置并且可以毫无问题地查询,其设置方式与我的新表“PersonList”相同。但是,当我尝试在PersonList上执行查询以选择某些值时,我收到错误
android.database.sqlite.SQLiteException: no such table: PersonList: , while compiling: SELECT ...
虽然我在SQLiteOpenHelper类的onCreate方法中执行了一个create查询,但它让我觉得该表从未创建过。与LocalLogin具有相同的数据库名称可能存在问题吗?
以下是相关代码:
数据库适配器类
public class GoingOutPersonListDbAdapter {
private static final String DATABASE_NAME = "GoingOutData";
private static final String DATABASE_TABLE_PERSONLIST = "PersonList";
private static final int DATABASE_VERSION = 1;
public static final String PERSONLIST_ID = "PersonList_id";
public static final String PERSONLIST_LOGIN = "Login";
public static final String PERSONLIST_PASSWORD = "Password";
private static final String TAG = "Debugstring";
private PersonListDatabaseHelper mPersonListDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"CREATE Table " + DATABASE_TABLE_PERSONLIST+ " ( "
+ PERSONLIST_ID + " integer PRIMARY KEY Autoincrement, "
+ PERSONLIST_LOGIN + " text NOT NULL,"
+ PERSONLIST_PASSWORD + " text NOT NULL );";
private final Context mCtx;
private static class PersonListDatabaseHelper extends SQLiteOpenHelper {
PersonListDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG,DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
}
public GoingOutPersonListDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public GoingOutPersonListDbAdapter open() throws SQLException {
mPersonListDbHelper = new PersonListDatabaseHelper(mCtx);
mDb = mPersonListDbHelper.getWritableDatabase();
return this;
}
public Cursor searchPerson(String searchString) {
return mDb.query(DATABASE_TABLE_PERSONLIST, new String[] {PERSONLIST_ID, PERSONLIST_LOGIN, PERSONLIST_PASSWORD}, searchString, null, null, null, null);
}
}
在我的活动课程中:
private GoingOutPersonListDbAdapter mPersonListDbHelper;
...
mPersonListDbHelper = new GoingOutPersonListDbAdapter(this);
mPersonListDbHelper.open();
...
//loginEditText is properly set
Cursor personList = mPersonListDbHelper.searchPerson(GoingOutPersonListDbAdapter.PERSONLIST_LOGIN + " = '" + loginEditText + "'");
startManagingCursor(personList);
答案 0 :(得分:5)
如果要添加新表,则需要增加数据库版本,以便触发onUpdate,并使用新表重新创建数据库。
答案 1 :(得分:0)
您两次声明'private static final String DATABASE_CREATE'。实际上,我很惊讶这个编译?也就是说,您应该将日志语句添加到db.execSQL(DATABASE_CREATE);打电话,看看它是否真的成功了。