How to package and release the designed sqllite and Android programs

Source: Internet
Author: User

A recent whim, trying to develop androidProgramThe exercise Project is a simple weather forecast program. The weather forecast naturally involves all regions across the country.CodeIn an SQLite database, 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, it will be hard in the Java code.
The Code of thousands of insert statements is obviously a dummies. After searching through multiple networks, we finally found a perfect solution.

After a project is created in eclipse,AssetsDirectory, 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. */@ over Ridepublic 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. dbstring 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 )). exists () = Fal Se) {// 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 the 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 blocke. printstacktrace () ;}} cursor. close ();}}

When the program starts, it will go back and check if the database file is not there. If it does not exist, it will copy the prepared database to whichDatabasesDirectory, and if the user uninstalls the program, the directory and database will also be uninstalled.

ReferenceArticle:
Using your own SQLite database in Android applications

Embed a database in the. APK of a distributed application [Android]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.