How to publish the SQLite database (dictionary. DB file) along with the APK File

Source: Internet
Author: User

You can copy the dictionary. DB file to the res/raw directory of the eclipse Android project, as shown in 1. All files in the Res/raw directory are not compressed, so that files in the directory can be extracted directly.
Use the opendatabase method to open the database file. If the file does not exist, the system automatically creates the/sdcard/dictionary directory and sets the dictionary in the Res/raw directory. copy the DB file to the/sdcard/dictionary directory. The implementation code of the opendatabase method is as follows:

Code
Private sqlitedatabase opendatabase () <br/>{< br/> try <br/>{< br/> // obtain a dictionary. absolute path of the DB file <br/> string databasefilename = database_path + "/" + database_filename; <br/> file dir = new file (database_path ); <br/> // If the/sdcard/dictionary directory exists, create this directory <br/> If (! Dir. exists () <br/> dir. mkdir (); <br/> // If the directory does not exist in the/sdcard/dictionary directory <br/> // dictionary. DB file, copy the file from the res/raw directory to the <br/> // SD card directory (/sdcard/dictionary) <br/> If (! (New file (databasefilename )). exists () <br/>{< br/> // obtain the encapsulated dictionary. inputstream object of the DB file <br/> inputstream is = getresources (). openrawresource (R. raw. dictionary); <br/> fileoutputstream Fos = new fileoutputstream (databasefilename); <br/> byte [] buffer = new byte [8192]; <br/> int COUNT = 0; <br/> // Start copying dictionary. DB file <br/> while (COUNT = is. read (buffer)> 0) <br/>{< br/> FOS. write (buffer, 0, count); <br/>}< br/> FOS. close (); <br/> is. close (); <br/>}< br/> // open the dictionary in the/sdcard/dictionary directory. DB file <br/> sqlitedatabase database = sqlitedatabase. openorcreatedatabase (<br/> databasefilename, null); <br/> return database; <br/>}< br/> catch (exception E) <br/>{< br/>}< br/> return NULL; <br/>}

Several constants are used in the opendatabase method. These constants are defined in the main class of the program. The Code is as follows:

Code
Public class main extends activity implements onclicklistener, textwatcher <br/>{< br/> private final string database_path = android. OS. environment <br/>. getexternalstoragedirectory (). getabsolutepath () <br/> + "/Dictionary"; <br/> private final string database_filename = "dictionary. DB "; <br/>} 

 

From http://www.eoeandroid.com/thread-34883-1-1.html

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.