gpt4 book ai didi

android - SQLite 没有创建第二个表

转载 作者:行者123 更新时间:2023-11-29 16:18:07 25 4
gpt4 key购买 nike

我遇到了一个问题,我想在我的数据库中创建第二个表。这是代码

public class CBDataBaseHelper {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "Recipe_Name";
public static final String KEY_CATEGORY = "Recipe_Category";
public static final String KEY_DESCRIPTION = "Recipe_Description";

public static final String KEY_ROWID2 = "_id";
public static final String fKEY_ROWID2 = "f_id";
public static final String KEY_NAME2 = "Ingredient_Name";

private static final String DATABASE_NAME = "Recipedb";
private static final String DATABASE_TABLE = "RecipeData";
private static final String DATABASE_TABLE2 = "IngredientData";
private static final int DATABASE_VERSION = 1;

private DBHelper myHelper;
private final Context mycontext;
private SQLiteDatabase mydatabase;

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

}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

db.execSQL("CREATE TABLE " + DATABASE_TABLE + "( " + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY + " TEXT NOT NULL,"
+ KEY_DESCRIPTION + " TEXT NOT NULL" + ") ");

db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + "( "
+ KEY_ROWID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME2 + " TEXT NOT NULL, "
+ fKEY_ROWID2 + " INTEGER , FOREIGN KEY " + "(" + fKEY_ROWID2 + ") " + "REFERENCES " + DATABASE_TABLE + " ( " + KEY_ROWID + " ))");

}





@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

db.execSQL("DROP IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP IF EXISTS " + DATABASE_TABLE2);
onCreate(db);

}
}

public CBDataBaseHelper(Context c){
mycontext = c;

}

public CBDataBaseHelper open() throws SQLException{
myHelper = new DBHelper(mycontext);
mydatabase = myHelper.getWritableDatabase();
return this;
}

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


public long createEntry(String name, String description, String category){

ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_CATEGORY, description);
cv.put(KEY_DESCRIPTION, category);
return mydatabase.insert(DATABASE_TABLE, null, cv);

}

public long createEntry2(String name){

ContentValues cv = new ContentValues();
cv.put(KEY_NAME2, name);
return mydatabase.insert(DATABASE_TABLE2, null, cv);

}

public Cursor query() {
// Open Database

String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_CATEGORY, KEY_DESCRIPTION};
Cursor cursor = mydatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return cursor;


}

public Cursor query2() {
// Open Database

String[] columns = new String[]{KEY_ROWID2, fKEY_ROWID2, KEY_NAME2};
Cursor cursor = mydatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);
return cursor;


}

public Cursor fetchRow(long rowId) throws SQLException {
Cursor mCursor = mydatabase.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
KEY_CATEGORY, KEY_DESCRIPTION }, KEY_ROWID + "="
+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;

}


public boolean updateRecipe(long rowId, String RecipeName,
String RecipeCategory, String RecipeDescription)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, RecipeName);
args.put(KEY_DESCRIPTION, RecipeDescription);
args.put(KEY_CATEGORY, RecipeCategory);

return mydatabase.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}

然后我有一个游标调用到这个数据库的第二个表,这是错误进来的,“没有这样的表”。有什么想法吗?

最佳答案

这就是问题所在,虽然它看起来有点傻。问题是你的 onCreate创建数据库没有被调用。我确定,但你可以确认这一点。还有你的onUpgrade没有被调用。原因是 DB 已经存在所以 onCreate不会打电话现在你想onUpgrade将被调用,但不会被调用,因为您没有更改 DATABASE_VERSION .

解决方案:

  • 通过命令实用程序“adb shell”或使用文件资源管理器通过 eclipse 删除数据库。数据库位于“/data/data//databases”下。

对于 adb 使用 rm <db_name>

其他解决方案,如果一些数据已经存在(您想要保留用于测试),这仅用于测试目的:

  • 尝试将 DATABASE_VERSION 更改为当前值以外的值。这将导致您的 onUpgrade被调用所以写你的sqls这样你就不会丢失数据。但显然你的作品onUpgrade将与这一个不同。

关于android - SQLite 没有创建第二个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452113/

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