I recently started learning Android programming and wrote a small project myself. One requirement is to initialize an SQLite database from a file. That is, I have a file containing a lot of SQL statements. I want my android program to execute this SQL statement and initialize the database when it is started for the first time. However, this SQL statement will not be executed in subsequent executions, it can be said that it is a very typical reference.
Google: the implementation of this task is this article ": using your own SQLite database in Android applications: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications /.
I tried his method and can use it, but there are two points that make me very uncomfortable:
1) Its database initialization does not use SQL statements, but uses a ready-made SQLite binary file to directly copy it to the database path of the Android system. On the one hand, I am not at ease with the direct copy of such binary files. On the other hand, if the binary structure of the SQLite database is upgraded or changed, my program will not be compatible with all SQLite versions.
2) Although the databasehelper class is extends sqliteopenhelper, it does not follow the original logic of the original sqliteopenhelper class, and does not reload the oncreate and onopen classes. It checks whether the database exists, open and Close functions such as databases. This makes sqliteopenhelper nearly meaningless for extension of this class. It almost re-constructs the application logic, so that I cannot use sqliteopenhelper. Getreadabledatabase and other functions are uncomfortable (maybe my cleanliness ).
After debugging, it is found that the oncreate of sqliteopenhelper is executed only when the program uses the database for the first time, and then oncreate is executed. Subsequent execution will skip oncreate and directly execute onopen. Therefore, you do not need to check whether the database is the most secure. Follow the "correct" usage method of sqliteopenhelper. You can use this subclass to initialize your android database. The database is stored in the Res/raw directory of the project in the form of SQL statements. Remember one statement per line (because I use Readline () to read the source file) and cannot wrap it! You can use a local database export tool. I use Firefox
Extension: SQLite manager.
/**
*
*/
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 Li youhua
*
*/
Public class filesqlitehelp extends sqliteopenhelper {
Private Static string db_path = "/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 ){
Super (context, db_name, null, 1 );
This. mycontext = context;
}
/**
* In‑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.exe csql (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
}
}
In fact, there are very few codes. That's enough. In this way, you can use sqliteopenhelper. getreadabledatabase () to use your database according to the normal logic.