Recently, I tried to develop the android program. The exercise Project is a simple weather forecast program. The weather forecast naturally involves various regions throughout the country. I put the names and region code of these regions in an SQLite database. Of course, Android also supports SQLite well. The problem is that many tutorials or examples are found during the learning process. After the program is run, a database is created instantly, tables are created, and data is inserted, my own idea is to prepare the database on the computer and then package and release it with the APK; otherwise, thousands of insert statements can be read hard in the Java code, it is obviously a dummies. After searching through multiple networks, we finally found a perfect solution.
After a project is created in eclipse,assets
Directory, copy the prepared SQLite database to this directory in eclipse, and then encode it in the main activity:
Package COM. test. DB; import Java. io. file; import Java. io. fileoutputstream; import Java. io. inputstream; import Java. io. outputstream; import Java. io. unsupportedencodingexception; import android. app. activity; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. OS. bundle; public class dbtestactivity extends activity {/** called when the activity is first created. */@ ov Erride public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // COM. test. DB is the package name of the program. Please adjust it according to your own program // data/COM. test. the database // databases directory is the place where the SQLite database is to be stored, and the default database storage directory of the android program. // The database name is test. DB string db_path = "/data/COM. test. DB/databases/"; string db_name =" test. DB "; // check whether the SQLite database file exists if (new file (db_path + db_name )). ex Ists () = false) {// If the SQLite database file does not exist, check whether the database directory contains file F = new file (db_path); // if the database directory does not exist, create this directory if (! F. exists () {f. mkdir ();} Try {// get the prepared SQLite database under the assets directory as the input stream inputstream is = getbasecontext (). getassets (). open (db_name); // output stream outputstream OS = new fileoutputstream (db_path + db_name); // write byte [] 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 (); is. close ();} catch (exception e) {e. printstacktrace () ;}/// test/data/COM. test. can databases under DB/databases/work properly? sqlitedatabase database = sqlitedatabase. openorcreatedatabase (db_path + db_name, null); cursor = database. rawquery ("select * from test", null); If (cursor. getcount ()> 0) {cursor. movetofirst (); try {// solve Chinese garbled problem byte test [] = cursor. getblob (0); string strtest = new string (test, "UTF-8 "). trim (); // check whether the output information is correct. out. println (strtest);} catch (unsupportedencodingexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} cursor. close ();}}
Source: http://www.akasuna.com/2012/03/09/embed-sqlite-database-in-the-apk-of-android-distributed-application/