Use an embedded relational SQLite database to store data

Source: Internet
Author: User

In addition to using files or SharedPreferences to store data, you can also choose to use the SQLite database to store data.
On the Android platform, an embedded relational database-SQLite is integrated. SQLite3 supports NULL, INTEGER, REAL (floating point number), TEXT (string TEXT), and BLOB (binary object) data Type. Although only five types are supported, sqlite3 also accepts data types such as varchar (n), char (n), and decimal (p, s,
It is only converted to the corresponding five data types during operation or storage.
The biggest feature of SQLite is that you can save various types of data to any field without worrying about the data type declared by the field;
SQLite can also parse SQL statements that are not part of it, so it is easy to use;
In SQLite, why does the Android system implement the abstract class of SQLiteOpenHelper? So I will implement the method to allow us to manage the data and create it in version management;
In the help class of SQLiteOpenHelper,
Let's implementOnCreate (SQLiteDatabase db), onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)These two methods
①:Public abstract void onCreate (SQLiteDatabase db)When the DataBase is created for the first time, the onCreate () method can generate the DataBase table structure and add the initialization data used by some applications, and return the DataBase
②:Public abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)Called when the database needs to be updated. You need to delete the original table or update the new version number before calling the database.
Of course we will also useGetReadableDatabase () and head WritableDatabase ()The two methods are used to obtain the data of an instance.
However, the getWritableDatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but not write;
The getReadableDatabase () method first opens the database in read/write mode. If the disk space of the database is full, opening fails,
However, we were lucky to have failed to open the database. We still tried to open the database in read-only mode.

Demo: using student information management as an Example
Student_Model:

  1. PackageCom. jiangqq. model;
  2. ImportJava. io. Serializable;
  3. /** 
  4. * Student model class 
  5. * @ Author jiangqq 
  6. */
  7. Public ClassStudentImplementsSerializable
  8. {
  9. PrivateInteger stu_id;
  10. PrivateString name;
  11. PrivateString gender;
  12. PrivateShort age;
  13. PrivateString telphone;
  14. PublicStudent (){
  15. Super();
  16. }
  17. PublicStudent (String name, String gender, Short age, String telphone ){
  18. Super();
  19. This. Name = name;
  20. This. Gender = gender;
  21. This. Age = age;
  22. This. Telphone = telphone;
  23. }
  24. PublicStudent (Integer id, String name, String gender, Short age,
  25. String telphone ){
  26. Super();
  27. This. Stu_id = id;
  28. This. Name = name;
  29. This. Gender = gender;
  30. This. Age = age;
  31. This. Telphone = telphone;
  32. }
  33. PublicInteger getId (){
  34. ReturnStu_id;
  35. }
  36. Public VoidSetId (Integer id ){
  37. This. Stu_id = id;
  38. }
  39. PublicString getName (){
  40. ReturnName;
  41. }
  42. Public VoidSetName (String name ){
  43. This. Name = name;
  44. }
  45. PublicString getGender (){
  46. ReturnGender;
  47. }
  48. Public VoidSetGender (String gender ){
  49. This. Gender = gender;
  50. }
  51. PublicShort getAge (){
  52. ReturnAge;
  53. }
  54. Public VoidSetAge (Short age ){
  55. This. Age = age;
  56. }
  57. PublicString getTelphone (){
  58. ReturnTelphone;
  59. }
  60. Public VoidSetTelphone (String telphone ){
  61. This. Telphone = telphone;
  62. }
  63. @ Override
  64. PublicString toString (){
  65. Return "Student [id ="+ Stu_id +", Name ="+ Name +", Gender ="
  66. + Gender +", Age ="+ Age +", Telphone ="+ Telphone +"]";
  67. }
  68. }

First, write the Database Help class:

  1. PackageCom. jiangqq. service;
  2. ImportAndroid. content. Context;
  3. ImportAndroid. database. sqlite. SQLiteDatabase;
  4. ImportAndroid. database. sqlite. SQLiteOpenHelper;
  5. Public ClassDBOpenHelperExtendsSQLiteOpenHelper
  6. {
  7. Private Static FinalString DB_NAME ="Student. db";
  8. Private Static Final IntVERSION =1;
  9. Private Static FinalString CREATE_TABLE ="Create table student (stu_id integer primary key autoincrement, name text, gender text, age integer, telphone text );";
  10. PublicDBOpenHelper (Context context ){
  11. Super(Context, DB_NAME,Null, VERSION );
  12. }
  13. @ Override
  14. Public VoidOnCreate (SQLiteDatabase db ){
  15. Db.exe cSQL (CREATE_TABLE );
  16. }
  17. @ Override
  18. Public VoidOnUpgrade (SQLiteDatabase db,IntOldVersion,IntNewVersion ){
  19. Db.exe cSQL ("Drop table if exists student");
  20. OnCreate (db );
  21. }
  22. }

At this point, the help class of the database has been basically completed, and the student information will be managed using methods in the SQLiteDatabase class;
  • 1
  • 2
  • 3
  • Next Page

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.