我正在尝试使用 rawQuery
和 execSQL
方法来操作我的数据库,而不是 .update
、.insert
等.我正在尝试使用以下代码执行更新:
I am trying to use rawQuery
and execSQL
methods for manipulating my database, instead of the .update
, .insert
, etc. I am trying to perform an UPDATE with the following code:
db.execSQL("UPDATE configuration " +
"SET number_fields = " + number_fields + ", "
+ "frequency = " + frequency + ", "
+ "ag = " + ag + ", "
+ "number_alarms = " + number_alarms + ", "
+ "failed_rapper = " + failed_rapper + ", "
+ "max_mv = " + max_mv + ", "
+ "max_nav = " + max_nav + " "
+ "WHERE serial_id = " + Integer.toString(id) + ";");
在此操作之后,有一个日志表明发生了更新并且它似乎可以工作,但是当我尝试在表上执行 select 语句时,它返回并显示以下错误:
AFter this action there is a log that states an update has occurred and it seems to work, yet when I try to do a select statement on the table, it returns with the following error:
06-10 10:01:47.564: W/System.err(3815): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
现在,我在 Android 中使用 SQLite 数据库浏览器手动插入的不同数据上执行了相同的 SELECT,它工作正常.我还在 SQLite 浏览器中执行了相同的 UPADTE,它在那里工作.因此,我知道问题在于 UPDATE 命令在 Android 上运行时不起作用.
Now I have performed that same SELECT on different data in Android that I manually inserted using SQLite Database Browser and it works fine. I have also performed that same UPADTE in the SQLite Browser and it has worked there. Therefore I know that the problem is that the UPDATE command is not working when running on Android.
问题:为什么 UPDATE 命令在 execSQL()
方法中不起作用?
Question: Why does the UPDATE command not work within the execSQL()
method?
来自Android SQLiteDatabase 类文档:
执行不是 SELECT 或任何其他 SQL 的单个 SQL 语句返回数据的语句.
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
没有办法返回任何数据(比如受影响的人数行).相反,我们鼓励您使用 insert(String, String,内容值),更新(字符串,内容值,字符串,字符串 [])等al,如果可能的话.
It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert(String, String, ContentValues), update(String, ContentValues, String, String[]), et al, when possible.
然后:
对于 UPDATE 语句,请改用以下任何一种.
For UPDATE statements, use any of the following instead.
更新(字符串,内容值,字符串,字符串[])
update(String, ContentValues, String, String[])
updateWithOnConflict(String, ContentValues, String, String[], int)
updateWithOnConflict(String, ContentValues, String, String[], int)
据我所知,execSQL
方法更多用于更高级别的数据库操作,例如创建表和更改架构,以及 .query
、.update
、.delete
等方法来修改行.除了 .update
来执行这个操作.
As far as I can tell, the execSQL
method is more for higher level database operations, such as creating tables and changing schema, and the .query
, .update
, .delete
, etc. methods should be used to modify rows. I'm not sure you have another option besides .update
to perform this operation.
这篇关于带有 UPDATE 的 execSQL() 不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!