【发布时间】:2019-12-30 01:36:19
【问题描述】:
我正在尝试向我的表中添加一行。看起来它工作得很好,但是该行没有添加到数据库中。当我检查数据库时,它没有任何变化。
我正在尝试使用 SqliteDatabase 提供的插入方法添加它,并提供以下代码。
public void saveToCache(String word) {
ContentValues contentValues = new ContentValues();
contentValues.put("word", word);
long value = database.insert("CACHE",null, contentValues);
//The code below is for debug purposes, to see the if values are added
//But when executed it gives
//android.database.CursorIndexOutOfBoundsException: Index -1 requested,
//with a size of 13
Cursor cursor = database.rawQuery("SELECT * FROM CACHE",null);
for(int i=0; i< cursor.getCount();i++){
String a = cursor.getString(0); //This is where the exception above happens
}
}
value 属性似乎是对的,每次我使用该函数时,它都会递增 1。但是下面的 rawQuery 方法给出了我在评论中提供的错误。
p.s : "CACHE" 表只有一个名为 word 的 COLUMN。
【问题讨论】:
-
在循环开始之前调用
cursor.moveToFirst()开始获取数据。 -
您是否配置了任何自动提交属性?可能是事务没有提交。
-
@S-Sh 谢谢!我现在可以看到这些值已添加到数据库中。但奇怪的是,当我使用 GUI、Sql Browser For Sqlite 检查它时,我看到表中没有任何变化。
-
@hmanolov 我对自动提交属性一无所知
标签: java android sqlite insert android-sqlite