gpt4 book ai didi

database - Android:数据库读取问题抛出异常

转载 作者:搜寻专家 更新时间:2023-10-30 21:37:35 25 4
gpt4 key购买 nike

我在使用 android 数据库时遇到了这个问题。我采用了来自 google android 页面的 DBAdapter 文件 NotepadAdv3 示例。

数据库适配器.java

public class DBAdapter {
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "PasswordDb";
private static final String DATABASE_TABLE = "myuserdata";
private static final String DATABASE_USERKEY = "myuserkey";
private static final int DATABASE_VERSION = 2;

public static final String KEY_USERKEY = "userkey";
public static final String KEY_TITLE = "title";
public static final String KEY_DATA = "data";
public static final String KEY_ROWID = "_id";

private final Context mContext;

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DB_CREATE_KEY =
"create table " + DATABASE_USERKEY
+ " ("
+ "userkey text not null"
+");";

private static final String DB_CREATE_DATA =
"create table " + DATABASE_TABLE
+ " ("
+ "_id integer primary key autoincrement, "
+ "title text not null"
+ "data text"
+");";

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS myuserkey");
db.execSQL("DROP TABLE IF EXISTS myuserdata");
onCreate(db);
}
}

public DBAdapter(Context ctx)
{
this.mContext = ctx;
}

public DBAdapter Open() throws SQLException{
try {
mDbHelper = new DatabaseHelper(mContext);
}
catch(Exception e){
Log.e(TAG, e.toString());
}
mDb = mDbHelper.getWritableDatabase();
return this;
}

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

public Long storeKey(String userKey){
ContentValues initialValues = new ContentValues();

initialValues.put(KEY_USERKEY, userKey);
try {
mDb.delete(DATABASE_USERKEY, "1=1", null);
}
catch(Exception e)
{
Log.e(TAG, e.toString());
}
return mDb.insert(DATABASE_USERKEY, null, initialValues);
}

public String retrieveKey() {
final Cursor c;
try {
c = mDb.query(DATABASE_USERKEY, new String[] {
KEY_USERKEY},
null,
null,
null,
null,
null);
}catch(Exception e){
Log.e(TAG, e.toString());
return "";
}

if(c.moveToFirst()){
return c.getString(0);
}
else{
Log.d(TAG, "UserKey Empty");
}
return "";
}
//not including any function related to "myuserdata" table

Class1.java

{
mUserKey = mDbHelper.retrieveKey();
mDbHelper.storeKey(Key);

我收到的错误来自方法 retrieveKey() 和 storeKey() 中的 Log.e(TAG, e.toString())

"no such table: myuserkey: , while compiling: SELECT userkey FROM myuserkey"

最佳答案

您是否弹出了 DB 版本以便 onUpgrade 触发?在上面的示例中,您使用的是版本 2,但如果您更改了自版本 2 以来的架构,那么您需要再次弹出该版本。

关于database - Android:数据库读取问题抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2543466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com