How to publish a SQLite database (dictionary.db file) with the APK file

Source: Internet
Author: User
Tags sqlite sqlite database

You can copy the Dictionary.db file to the Res\raw directory in the Eclipse Android project, as shown in 1. All files in the Res\raw directory are not compressed, so that the files in that directory can be extracted directly.
Use the OpenDatabase method to open the database file, and if the file does not exist, the system automatically creates the/sdcard/dictionary directory and copies the dictionary.db files in the Res\raw directory to the/sdcard/ The dictionary directory. The implementation code for the OpenDatabase method is as follows:


Code
Private Sqlitedatabase OpenDatabase ()
{
Try
{
Get the absolute path to the dictionary.db file
String DatabaseFileName = Database_path + "/" + database_filename;
File dir = new file (Database_path);
If the/sdcard/dictionary directory exists, create this directory
if (!dir.exists ())
Dir.mkdir ();
If it does not exist in the/sdcard/dictionary directory
dictionary.db file, copy the file from the Res\raw directory to
Directory of SD cards (/sdcard/dictionary)
if (! ( New File (DatabaseFileName)). Exists ())
{
Get the InputStream object that encapsulates the Dictionary.db file
InputStream is = Getresources (). Openrawresource (R.raw.dictionary);
FileOutputStream fos = new FileOutputStream (databasefilename);
byte[] buffer = new byte[8192];
int count = 0;
Start copying dictionary.db files
while ((count = is.read (buffer)) > 0)
{
Fos.write (buffer, 0, count);
}

Fos.close ();
Is.close ();
}
Open the Dictionary.db file in the/sdcard/dictionary directory
Sqlitedatabase Database = Sqlitedatabase.openorcreatedatabase (
DatabaseFileName, NULL);
return database;
}
catch (Exception e)
{
}
return null;
}





Several constants are used in the OpenDatabase method, which are defined in the main class (main) of the program, and the code is as follows:



Code
public class Main extends Activity implements Onclicklistener, Textwatcher
{
Private final String Database_path = android.os.Environment
. getExternalStorageDirectory (). GetAbsolutePath ()
+ "/dictionary";
Private final String database_filename = "dictionary.db";
}

How to publish a SQLite database (dictionary.db file) with the APK file

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.