Initializing SQLite database with files in Android (ii)

Source: Internet
Author: User

BoAndroidsqlite Database Initialization at startup

Method 1 has described a way to initialize a database

Its database initialization is not a SQL statement, but a ready-made SQLite binary file for direct copy to the Android system's database path. On the one hand, I'm not so sure. Direct copy of this binary file, on the other hand, if the binary structure of the SQLite database has been upgraded or changed, my program will not be compatible with all SQLite versions.

Method 2: Execute the SQL file to initialize the database at startup.

Sqliteopenhelper's OnCreate executes only when the program first uses the database, then executes the oncreate, and thereafter the execution skips OnCreate and executes OnOpen directly. So there is no need to detect the database at all.

Following the "correct" use method of Sqliteopenhelper, I write the Sqliteopenhelper subclass that you can use to initialize your Android database. Database in the form of SQL statements, placed under the Res/raw directory of the project, remember each line of a statement (because I use ReadLine () to read the source file), can not wrap! You can use the Local database export tool, where data export is omitted.

The code is as follows:

Java code
  1. /**
  2. *
  3. */
  4. Package com.yourpackage;
  5. Import Java.io.BufferedReader;
  6. Import Java.io.FileWriter;
  7. Import java.io.IOException;
  8. Import Java.io.InputStream;
  9. Import Java.io.InputStreamReader;
  10. Import Android.content.Context;
  11. Import Android.database.sqlite.SQLiteDatabase;
  12. Import Android.database.sqlite.SQLiteOpenHelper;
  13. /**
  14. * @author Fan.zhang
  15. *
  16. */
  17. Public class Databasehelper extends Sqliteopenhelper {
  18. private static String Db_path = "/data/data/your_package_name/databases/";
  19. private static final String db_name = "Your_db_name.db";
  20. private Sqlitedatabase myDataBase;
  21. Private final Context mycontext;
  22. Public Filesqlitehelp (context context) {
  23. Super (context, db_name, null, 1);
  24. This.mycontext = context;
  25. }
  26. /** 
  27. * Inital your database from your local res-raw-folder to the just created
  28. * Empty database in the System folder, from where it can be accessed and
  29. * Handled.
  30. * */
  31. private void Initdatabase ()throws IOException {
  32. //Open your local db as the input stream
  33. //InputStream myinput = Mycontext.getassets (). open (db_name);
  34. InputStream myinput = Mycontext.getresources (). Openrawresource (
  35. R.raw.your_db_file_name);
  36. InputStreamReader reader = new InputStreamReader (myinput);
  37. BufferedReader Breader = new BufferedReader (reader);
  38. //Path to the just created empty db
  39. String outfilename = Db_path + db_name,
  40. Str
  41. //Open The empty db as the output stream
  42. FileWriter myoutput = new FileWriter (Outfilename, true);
  43. While ((str = breader.readline ()) = null) {
  44. Mydatabase.execsql (str);  //exec your SQL line by line.
  45. }
  46. //Close the streams
  47. Myoutput.flush ();
  48. Myoutput.close ();
  49. Myinput.close ();
  50. }
  51. /* 
  52. * (Non-javadoc)
  53. *
  54. * @see
  55. * Android.database.sqlite.sqliteopenhelper#oncreate (Android.database.sqlite
  56. * . Sqlitedatabase)
  57. */
  58. @Override
  59. public void OnCreate (Sqlitedatabase db) {
  60. //TODO auto-generated method stub
  61. MyDataBase = db;
  62. try {
  63. this.initdatabase ();
  64. } catch (IOException e) {
  65. //TODO auto-generated catch block
  66. E.printstacktrace ();
  67. }
  68. }
  69. @Override
  70. public void OnOpen (Sqlitedatabase db) {
  71. Boolean readOnly = Db.isreadonly ();
  72. }
  73. /* 
  74. * (Non-javadoc)
  75. *
  76. * @see
  77. * Android.database.sqlite.sqliteopenhelper#onupgrade (Android.database.sqlite
  78. * . sqlitedatabase, int, int)
  79. */
  80. @Override
  81. public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
  82. //TODO auto-generated method stub
  83. }
  84. }

You can use your database with databasehelper.getreadabledatabase () as normal logic.

Initializing SQLite database with files in Android (ii)

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.