使用execSQL在Android的SQLite的INSERT操作操作、Android、execSQL、INSERT

2023-09-07 09:00:58 作者:凉墨&

从文档和This帖子我知道的 execSQL()执行一个SQL语句不是SELECT或返回数据的任何其他SQL语句。它也提到execSQL不应被用于插入。但我使用execSQL()用于插入这样的:

From 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

这是工作完美的我(没有与此不INSERT SELECT语句),但我被告知不要使用此Here.所以,我的问题是:做即使有附加与它没有任何的SELECT语句INSERT语句返回值请帮忙

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 INSERT statement returns value even though there is no SELECT statement attached with it? Please help.

推荐答案

您可以使用它,它只是不建议,因为您将不会收到可能由DB产生的任何信息。这些方法你应该使用return codeS /光标/等信息,让你知道发生了什么,所以你可以对付他们,如果有问题。

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.

关键是这一行中的 execSQL 文档:

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...

重要的词有鼓励。您的可以的使用 execSQL 任何SQL命令,但你对过程没有反馈,这使得它相当危险的用的东西,你应该得到的反馈(除非你不在乎比插入失败,或只有5 2条记录得到了更新)。

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).