带有 UPDATE 的 execSQL() 不更新

编程入门 行业动态 更新时间:2024-10-27 08:36:03
本文介绍了带有 UPDATE 的 execSQL() 不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 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() 不更新

本文发布于:2023-06-08 14:30:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/583102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UPDATE   execSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!