After the new project is built in Eclipse, there will be an Assets directory by default. In Eclipse, the prepared SQLite database is copied to the directory, and then encoded in the main Activity:
package com.test.db; Import java.io.file; Import Java.FileoutPutstream; Import java.Io.inputStream; Import Java.io.OutputStream; Import .SupportedEdEncodingException; Import android.app.Activity; Import Android.database.cursor; Import android.dataBase.sqlite.sqlitedatabase; Import Android.os.bundle; Public Class DBTESTATITITITITITE ITY { / ** Called when the activity is first created.* / @Override Public Void Oncreate (Bundle Savedancestate) {Super.Oncreate (SAVEDINSTANCESTATE); SetContentView (R.Layout.main); // Com.test.db is the package name of the program, please adjust according to your program // /data/data/com.test.db/// /DataBases directory is where the SQLite database is ready, and the default database storage directory of the Android program // The database is named Test.db String db_path = "/data/com.test.db/dataBases/"; String db_name = " test.db "; // Check if the SQLite database file exists if (New File (DB_PATH + DB_NAME)). Exist () == False) {// If the Sqlite database file does not exist, check whether the database directory exists with file f = new file (db_path); // If the database directory does not exist, the new directory IF (! f.exist ()) {f.mkdir ();} Try {// get the sqlite that we realize in the assets directory. The database is used as an input stream inputStream is = getBaseContext (). Getassets (). Open (db_name); // OutputStream OS = New FileoutStream (db_path + db_name); e [] buffer = new byte [1024] ; int length; while (length = is.read (buffer)> 0) {OS.Write (buffer, 0, length);} // Close the file stream os.flush (); os.close (); .close ();} Catch (Exception E) {e.printstacktrace ();} // Test /d test /data/data/com.test.db/databases/ Whether the database can work normally sqlitedAbase = Sqlited ABASE.OPENORCREATEDABASE (DB_PATH + DB_NAME, null); Cursor cursor = database.rawQuery("select * from test", null); if (cursor.getCount() > 0) { cursor.moveToFirst(); try { // Solve the problem of Chinese garbled Chinese byte test[] = cursor.getBlob(0); String strtest = new String(test, "utf-8").trim(); // Check whether the output information is correct System.out.println(strtest); } catch (UnupportedEndenCodingException E) {// Todo Auto-Generatd Catch Block E.printstacktrace ();}} Cursor.Close ();}}When the program starts, go back to check the database files. If it does not exist, it will copy which DataBases directory we prepare to is, and if the user uninstall this program, then this directory and database will be uninstalled.
Let's have another example.
The normal application database is stamped at/data/data/package name /database/test.db. When the application is published, this database will not be released with the application.
Therefore, in order to allow the data that we have already prepared, we must be able to copy the database itself to the SD card.
Implement the resource copy of the resource under the/mNDCard/test/test.db of the resource under the copy of the resource under the rested resida
The code is as follows:
Package zcping.syan.dbdefinition; Import Java.iO.File; Import Java.filenotFoundexception; Import Java.fileouTputStream; Import Java.i. OException; Import java.io.inputStream; Import Zcping.Syan.DragonBaby.r ; Import Android.Content.Context; Import Android.database.sqlite.sqlitedatabase; Import Android.util.log; Public Class iTy { /** Called when the activity is first created.*// SD card directory Private Final String database_path = Android.os.environment .GetextRAGEDIRECTORY (). GetabsolutePath ()/"/db_exam"; Lename = "db_exam.db"; // This context is necessary, no context, why The copy operation of the database cannot be implemented; the constructor must be introduced in the context. } Public SqlitedAbase OpenDataBase () {Try {string databasefilename = database_path + "/" + database_filename; file dir = new file (database_path); // Determine whether there is a directory with the database under the SD card. If it does not exist, the new directory IF ( ! Dir.exists () ) {dir.mkdir (); Log.i ("ReleaseDataBaseActivity", "Dir Made:" + DataBase_path); Abase_path);} Try {// If the database has existed in the SD card directory, then there is no need to re -create, otherwise the file will be created and copy the database file IF (! (New File (DataBaseFilename)). Exist ()) {log.i) ; e (R.RAW.DB_EXAM); // Test int size = is.available (); Log.i ("ReleaseDataBaseActivity", "DataBase_size:" + 1); Log.i ("ReleaseDataBaseActivity", "Count:" + 0); Information flow FileoutStream FOS = New FileoutPutstream (DataBaseFilename); byte [] buffer = New Byte [8192]; int Count = 0; log.i ("ReleaseDataActivity", " Count: " + Count); // Write the data into the SD card directory Under the whole ((count = is.read (buffer))> 0) {fos.write (buffer, 0, count);} fos.flush (); fos.close (); is.Close (); (FilenotFoundexception E) {log.e ("database", "file not found"); ); E.printstacktrace ();} // Instantly the database on the SD card. DataBase is used as the return value. It is the excuse for all the insertions, delete, and query operations. Sqlitedatabase database = sqliteddatabase.openorcreatedatabase (databasefilename, null); Return database;} Catch (Exception E) {} Return Null; } After testing, it is absolutely easy to make. I hope it will be helpful to everyone.