Android SQLite insert optimization sqlitestatement

Source: Internet
Author: User

Recently, due to the needs of the project, it is necessary to improve the data warehouse receiving performance in the project. Through optimization, it is found that the warehouse receiving method is different, and the efficiency is really quite different.

The initial code is as follows: Execute SQL statements directly, and add transactions to improve performance:
Java code

  1. Sqlitedatabase database = new sqlitedatabase ();
  2. If (database. isopen ())
  3. {
  4. Database. begintransaction ();
  5. Try {
  6. // The SQL statement is insert into tablename (name) values ("test ")
  7. Database.exe csql (SQL );
  8. }
  9. Database. settransactionsuccessful ();
  10. } Finally {
  11. Database. endtransaction ();
  12. }
  13. Database. Close ();
  14. }




The optimization is as follows:
Java code

  1. Sqlitedatabase database = new sqlitedatabase ();
  2. // SQL is insert into tablename (name) values (?)
  3. Sqlitestatement sqlliststatment = database. compilestatement (SQL );
  4. If (database. isopen ())
  5. {
  6. Database. begintransaction ();
  7. Try {
  8. // The index starts from 1 and the value is the value of the database.
  9. // Bingxxx is the insert XXX type
  10. Sqlitestatement. bindstring (index, value );
  11. Sqlitestatement.exe cuteinsert ();
  12. }
  13. Database. settransactionsuccessful ();
  14. } Finally {
  15. Database. endtransaction ();
  16. }
  17. Database. Close ();
  18. }


From http://woodn-z.iteye.com/blog/1447719
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.