gpt4 book ai didi

java - 如何升级用户手机中的sqlite数据库

转载 作者:行者123 更新时间:2023-12-04 09:39:31 24 4
gpt4 key购买 nike

我正在尝试通过向现有数据库添加更多数据来升级 sqlite 数据库。 db 安装在用户的手机上。我的 db 版本目前是 1,我正在尝试升级到 2。我没有创建 onCreate 方法,因为我已经在其他应用程序创建的 Assets 文件夹中拥有了 sqlite db。我是 sqlite 的新手。我用谷歌搜索了这个错误,但我不知道如何解决这个错误。任何帮助,将不胜感激。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.donghyouny.biblecard, PID: 10475
java.lang.IllegalStateException: getDatabase called recursively
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:431)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:391)
at com.donghyouny.biblecard.MyDatabaseHelper.insertToBible(MyDatabaseHelper.java:68)
at com.donghyouny.biblecard.MyDatabaseHelper.onUpgrade(MyDatabaseHelper.java:62)
class MyDatabaseHelper extends SQLiteOpenHelper {

private Context context;
private static final String DATABASE_NAME = "bible.db";
private static final int DATABASE_VERSION = 2;

private static final String TABLE_SAVE = "save";
private static final String TABLE_BIBLE = "bible";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TIMESTAMP = "TimeStamp";
private static final String COLUMN_BIBLETYPE = "BibleType";
private static final String COLUMN_VERSE = "Verse";
private static final String COLUMN_CONTENT = "Content";
private static final String COLUMN_NUM = "Num";
private static final String COLUMN_CNUM = "CNum";
private static final String COLUMN_CHECKNUM = "CheckNum";
private static final String COLUMN_IMAGE = "Image";
private static final String COLUMN_BIBLEID = "BibleId";

MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
if(oldversion==1){
insertToBible("Bible", "Psalm 1,1-2", "I am the way and truth",9, 3, 0, imageViewToByte(28));
}
}

void insertToBible(String bibleType, String verse, String content, int num, int cnum, int checknum, byte[] image){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();

cv.put(COLUMN_BIBLETYPE, bibleType);
cv.put(COLUMN_VERSE, verse);
cv.put(COLUMN_CONTENT, content);
cv.put(COLUMN_NUM, num);
cv.put(COLUMN_CNUM, cnum);
cv.put(COLUMN_CHECKNUM, checknum);
cv.put(COLUMN_IMAGE, image);
cv.put(COLUMN_TIMESTAMP, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
long result = db.insert(TABLE_SAVE,null, cv);
if(result == -1){

}else {

}
}

最佳答案

更改 insertToBible()的签名作为第一个参数包含 SQLiteDatabase 的实例并删除:

SQLiteDatabase db = this.getWritableDatabase();

所以它是这样的:
void insertToBible(SQLiteDatabase db, String bibleType, String verse, String content, int num, int cnum, int checknum, byte[] image)

当你调用它时使用 onUpgrade() SQLiteDatabase 的实例:
insertToBible(db, "Bible", "Psalm 1,1-2", "I am the way and truth",9, 3,  0, imageViewToByte(28));

关于java - 如何升级用户手机中的sqlite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62394388/

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