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
- /**
- *
- */
- Package com.yourpackage;
- Import Java.io.BufferedReader;
- Import Java.io.FileWriter;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Java.io.InputStreamReader;
- Import Android.content.Context;
- Import Android.database.sqlite.SQLiteDatabase;
- Import Android.database.sqlite.SQLiteOpenHelper;
- /**
- * @author Fan.zhang
- *
- */
- Public class Databasehelper extends Sqliteopenhelper {
- private static String Db_path = "/data/data/your_package_name/databases/";
- private static final String db_name = "Your_db_name.db";
- private Sqlitedatabase myDataBase;
- Private final Context mycontext;
- Public Filesqlitehelp (context context) {
- Super (context, db_name, null, 1);
- This.mycontext = context;
- }
- /**
- * Inital your database from your local res-raw-folder to the just created
- * Empty database in the System folder, from where it can be accessed and
- * Handled.
- * */
- private void Initdatabase ()throws IOException {
- //Open your local db as the input stream
- //InputStream myinput = Mycontext.getassets (). open (db_name);
- InputStream myinput = Mycontext.getresources (). Openrawresource (
- R.raw.your_db_file_name);
- InputStreamReader reader = new InputStreamReader (myinput);
- BufferedReader Breader = new BufferedReader (reader);
- //Path to the just created empty db
- String outfilename = Db_path + db_name,
- Str
- //Open The empty db as the output stream
- FileWriter myoutput = new FileWriter (Outfilename, true);
- While ((str = breader.readline ()) = null) {
- Mydatabase.execsql (str); //exec your SQL line by line.
- }
- //Close the streams
- Myoutput.flush ();
- Myoutput.close ();
- Myinput.close ();
- }
- /*
- * (Non-javadoc)
- *
- * @see
- * Android.database.sqlite.sqliteopenhelper#oncreate (Android.database.sqlite
- * . Sqlitedatabase)
- */
- @Override
- public void OnCreate (Sqlitedatabase db) {
- //TODO auto-generated method stub
- MyDataBase = db;
- try {
- this.initdatabase ();
- } catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- @Override
- public void OnOpen (Sqlitedatabase db) {
- Boolean readOnly = Db.isreadonly ();
- }
- /*
- * (Non-javadoc)
- *
- * @see
- * Android.database.sqlite.sqliteopenhelper#onupgrade (Android.database.sqlite
- * . sqlitedatabase, int, int)
- */
- @Override
- public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
- //TODO auto-generated method stub
- }
- }
You can use your database with databasehelper.getreadabledatabase () as normal logic.
Initializing SQLite database with files in Android (ii)