execSQL()与更新不更新 [英] execSQL() with UPDATE doesn't update

查看:325
本文介绍了execSQL()与更新不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用操纵我的数据库 rawQuery execSQL 方法,而不是 .update .insert ,等我试图执行与以下code的更新:

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

现在我已经完成了同样的选择上,我使用SQLite数据库浏览器手动插入的Andr​​oid不同的数据,它工作正常。我也执行了同样的UPADTE在SQLite的浏览器,它一直在那里。因此,我知道,问题是,在Android上运行时,UPDATE命令无法正常工作。

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?

推荐答案

从<一个href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL%28java.lang.String%29\"相对=nofollow> Android的SQLiteDatabase类的文档:

执行一个SQL语句不是SELECT,或任何其它SQL
  语句返回的数据。

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

它没有装置返回任何数据(例如,受影响的数
  行)。相反,我们鼓励你使用insert(字符串,字符串,
  ContentValues​​),更新(字符串,ContentValues​​,字符串,字符串[]),等。
  人,如果可能的话。

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.

更新(字符串,ContentValues​​,字符串,字符串[])

update(String, ContentValues, String, String[])

updateWithOnConflict(字符串,ContentValues​​,字符串,字符串[],INT)

updateWithOnConflict(String, ContentValues, String, String[], int)

据我所知道的, execSQL 方法多为更高级别的数据库操作,如创建表和不断变化的模式,而 .query .update .delete 等方法应该用于修改行。我不知道你有另一种选择,除了<一个href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#update%28java.lang.String,%20android.content.ContentValues,%20java.lang.String,%20java.lang.String%5B%5D%29\"相对=nofollow> .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.

这篇关于execSQL()与更新不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆