Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

android - execSQL() with UPDATE doesn't update

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) + ";");

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

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.

Question: Why does the UPDATE command not work within the execSQL() method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the Android SQLiteDatabase class documentation:

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

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.

Then later:

For UPDATE statements, use any of the following instead.

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...