Sometimes our software used to SQLite database, this time how to pack a good database into our apk, in fact, is very simple, is to put our database files in our mobile phone, so do not have to limit where to write this code, in the first time to create a database can be, I think in the Software start Page effect is better, first we should put the pre-written database files such as test.db in the Res folder in the Raw folder, can also be put into the assets, because these two folders will not be generated when the APK is not compressed
ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportCom.android.qufu.dinner.MealActivityGroup;ImportCOM.ANDROID.QUFU.DINNER.R;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.content.pm.ActivityInfo;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteException;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public classLogginextendsActivity { Public StaticString dbname= "Dinner.db";//the name of the database Private StaticString database_path= "/data/data/com.android.qufu.dinner/databases/";//the path of the database in the phone intAlpha = 255; intb = 0; Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.login); //determine if the database exists BooleanDbexist =checkdatabase (); if(dbexist) {}Else{//If it doesn't exist, write the database in raw to the phone. Try{copydatabase (); }Catch(IOException e) {Throw NewError ("Error copying database"); } } NewThread (NewRunnable () { Public voidrun () {Initapp ();//Initialization Program while(B < 2) { Try { if(b = = 0) {Thread.Sleep (20); b= 1; } Else{Thread.Sleep (50); } Updateapp (); } Catch(interruptedexception e) {e.printstacktrace (); }}}). Start (); } Public voidUpdateapp () {Alpha-= 5; if(Alpha <= 0) {b= 2; if(true){ Try{Intent in=NewIntent (Loggin. This, Mealactivitygroup.class); Loggin. This. StartActivity (in); Loggin. This. Finish (); }Catch(Exception e) {}}} } /*** Determine if the database exists *@returnfalse or True*/ Public Booleancheckdatabase () {sqlitedatabase CheckDB=NULL; Try{String DatabaseFileName= database_path+DbName; CheckDB=sqlitedatabase.opendatabase (DatabaseFileName,NULL, sqlitedatabase.open_readonly); }Catch(Sqliteexception e) {}if(checkdb!=NULL) {checkdb.close (); } returnCheckDB! =NULL?true:false; } /*** Copy the database to the phone under the specified folder *@throwsIOException*/ Public voidCopydatabase ()throwsioexception{String databasefilenames=database_path+DbName; File dir=NewFile (Database_path); if(!dir.exists ())//determines whether a folder exists, does not exist, creates a newDir.mkdir (); FileOutputStream OS=NULL; Try{OS=NewFileOutputStream (Databasefilenames);//get the write stream of the database file}Catch(FileNotFoundException e) {e.printstacktrace (); } InputStream is= Loggin. This. Getresources (). Openrawresource (R.raw.test);//get the data stream of the database file byte[] buffer =New byte[8192]; intCount = 0; Try{ while(Count=is.read (buffer)) >0) {os.write (buffer,0, Count); Os.flush (); } }Catch(IOException e) {}Try{is.close (); Os.close (); }Catch(IOException e) {e.printstacktrace (); } } /*** Initialize, this is the start page is useless*/ Public voidInitapp () {}}
Reference: http://www.oschina.net/code/snippet_203635_9207
Android database package with APK release