Common SQLite operations in Android

Source: Internet
Author: User

Some bugs are being modified recently. If you think of them, you can sort out common errors so that you can quickly find out the cause of the error more quickly next time and provide solutions for those who encounter the same problem. I will classify the errors. I will write a little bit today and then add them later. please correct me. (In order not to disclose some information, the name of the log package has been modified)


1.Android. database. SQLite. sqliteexception: unable to close due to unfinalised statements


[Java]
View plaincopy
  1. Fatal exception: Main
  2. Java. Lang. runtimeexception: unable to start activity componentinfo {com. Evaluation/COM. Evaluation. vehicleactivity}: Android. database. SQLite. sqliteexception: unable to close due to unfinalised statements
  3. At Android. App. activitythread. javasmlaunchactivity (activitythread. Java: 2781)
  4. At Android. App. activitythread. handlelaunchactivity (activitythread. Java: 2797)
  5. At Android. App. activitythread. Access $2300 (activitythread. Java: 135)
  6. At Android. App. activitythread $ H. handlemessage (activitythread. Java: 2132)
  7. At Android. OS. handler. dispatchmessage (handler. Java: 99)
  8. At Android. OS. lorule. Loop (lorule. Java: 143)
  9. At Android. App. activitythread. Main (activitythread. Java: 4914)
  10. At java. Lang. Reflect. method. invokenative (native method)
  11. At java. Lang. Reflect. method. Invoke (method. Java: 521)
  12. At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 868)
  13. At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 626)
  14. At Dalvik. system. nativestart. Main (native method)
  15. Caused by: Android. database. SQLite. sqliteexception: unable to close due to unfinalised statements
  16. At Android. database. SQLite. sqlitedatabase. dbclose (native method)
  17. At Android. database. SQLite. sqlitedatabase. onallreferencesreleased (sqlitedatabase. Java: 363)
  18. At Android. database. SQLite. sqliteclosable. releasereference (sqliteclosable. Java: 45)
  19. At Android. database. SQLite. sqliteprogram. onallreferencesreleased (sqliteprogram. Java: 116)
  20. At Android. database. SQLite. sqliteclosable. releasereference (sqliteclosable. Java: 45)
  21. At Android. database. SQLite. sqliteprogram. Close (sqliteprogram. Java: 293)
  22. At Android. database. SQLite. sqlitequery. Close (sqlitequery. Java: 133)
  23. At Android. database. SQLite. sqlitecursor. Close (sqlitecursor. Java: 532)
  24. At com. databasehelper. selectassessinformation (databasehelper. Java: 338)
  25. At com. jskapplication. selectassessinformation (jskapplication. Java: 631)
  26. At com. vehicleactivity. oncreate (vehicleactivity. Java: 548)
  27. At Android. App. instrumentation. callactivityoncreate (instrumentation. Java: 1065)
  28. At Android. App. activitythread. javasmlaunchactivity (activitythread. Java: 2745)
  29. ... 11 more

This error is translated as: the database cannot be closed due to incomplete statements.

I checked it online. The most likely reason is that you have used multithreading when operating the SQLite database, but the SQLite database does not support multithreading, therefore, you must implement multi-threaded synchronization.

My program failed because I started a service in the background to request data from the server and insert data into the database in the thread. During a certain period of time, when a user inserts data into the database and closes the database, the thread is writing data to the database, which will cause the above exception.


2.Android. database. SQLite. sqliteconstraintexception: Error Code 19: constraint failed


[Java]
View plaincopy
  1. 07-04 16:18:56. 677: W/system. Err (29830): Android. database. SQLite. sqliteconstraintexception: Error Code 19: constraint failed

Constraints failed, causing such errors. Generally, there are two reasons:

1) One of the inserted data is the primary key, and the inserted primary key is the same;

2) if one piece of inserted data is empty and the database definition cannot be empty, this will also lead to such an error;

Some bugs are being modified recently. If you think of them, you can sort out common errors so that you can quickly find out the cause of the error more quickly next time and provide solutions for those who encounter the same problem. I will classify the errors. I will write a little bit today and then add them later. please correct me. (In order not to disclose some information, the name of the log package has been modified)


1.Android. database. SQLite. sqliteexception: unable to close due to unfinalised statements


[Java]
View plaincopy
  1. Fatal exception: Main
  2. Java. Lang. runtimeexception: unable to start activity componentinfo {com. Evaluation/COM. Evaluation. vehicleactivity}: Android. database. SQLite. sqliteexception: unable to close due to unfinalised statements
  3. At Android. App. activitythread. javasmlaunchactivity (activitythread. Java: 2781)
  4. At Android. App. activitythread. handlelaunchactivity (activitythread. Java: 2797)
  5. At Android. App. activitythread. Access $2300 (activitythread. Java: 135)
  6. At Android. App. activitythread $ H. handlemessage (activitythread. Java: 2132)
  7. At Android. OS. handler. dispatchmessage (handler. Java: 99)
  8. At Android. OS. lorule. Loop (lorule. Java: 143)
  9. At Android. App. activitythread. Main (activitythread. Java: 4914)
  10. At java. Lang. Reflect. method. invokenative (native method)
  11. At java. Lang. Reflect. method. Invoke (method. Java: 521)
  12. At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 868)
  13. At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 626)
  14. At Dalvik. system. nativestart. Main (native method)
  15. Caused by: Android. database. SQLite. sqliteexception: unable to close due to unfinalised statements
  16. At Android. database. SQLite. sqlitedatabase. dbclose (native method)
  17. At Android. database. SQLite. sqlitedatabase. onallreferencesreleased (sqlitedatabase. Java: 363)
  18. At Android. database. SQLite. sqliteclosable. releasereference (sqliteclosable. Java: 45)
  19. At Android. database. SQLite. sqliteprogram. onallreferencesreleased (sqliteprogram. Java: 116)
  20. At Android. database. SQLite. sqliteclosable. releasereference (sqliteclosable. Java: 45)
  21. At Android. database. SQLite. sqliteprogram. Close (sqliteprogram. Java: 293)
  22. At Android. database. SQLite. sqlitequery. Close (sqlitequery. Java: 133)
  23. At Android. database. SQLite. sqlitecursor. Close (sqlitecursor. Java: 532)
  24. At com. databasehelper. selectassessinformation (databasehelper. Java: 338)
  25. At com. jskapplication. selectassessinformation (jskapplication. Java: 631)
  26. At com. vehicleactivity. oncreate (vehicleactivity. Java: 548)
  27. At Android. App. instrumentation. callactivityoncreate (instrumentation. Java: 1065)
  28. At Android. App. activitythread. javasmlaunchactivity (activitythread. Java: 2745)
  29. ... 11 more

This error is translated as: the database cannot be closed due to incomplete statements.

I checked it online. The most likely reason is that you have used multithreading when operating the SQLite database, but the SQLite database does not support multithreading, therefore, you must implement multi-threaded synchronization.

My program failed because I started a service in the background to request data from the server and insert data into the database in the thread. During a certain period of time, when a user inserts data into the database and closes the database, the thread is writing data to the database, which will cause the above exception.


2.Android. database. SQLite. sqliteconstraintexception: Error Code 19: constraint failed


[Java]
View plaincopy
  1. 07-04 16:18:56. 677: W/system. Err (29830): Android. database. SQLite. sqliteconstraintexception: Error Code 19: constraint failed

Constraints failed, causing such errors. Generally, there are two reasons:

1) One of the inserted data is the primary key, and the inserted primary key is the same;

2) if one piece of inserted data is empty and the database definition cannot be empty, this will also lead to such an error;

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.