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

Categories

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

database - Using execSQL for INSERT operation in Android SQLite

From the documentation and this post I know execSQL() executes a single SQL statement that is not a SELECT or any other SQL statement that returns data. It is also mentioned that execSQL() should not be used for inserting.

But I'm using execSQL() for inserting like:

db.execSQL("INSERT INTO LIST VALUES('???');"); //where LIST is a table

This is working perfectly for me (there is no SELECT statement with this INSERT), but I was told not to use this here.

So, my question is: does the INSERT statement return a value even though there is no SELECT statement attached to it?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can use it, it is just not recommended because you won't receive any information that might be generated by the DB. The methods you are supposed to use return codes/cursors/other info to let you know what happened so you can deal with them if there is a problem.

The key is this line in the execSQL docs:

It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert...

The important word there is "encouraged". You can use execSQL for any SQL command, but you get no feedback on the process, which makes it rather dangerous to use for something where you should get feedback (unless you don't care than an insert failed, or only 2 of 5 records got updated).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...