SQLite database and APK application released at the same time

Source: Internet
Author: User

The farmer's uncle wrote quite well, you can read: http://www.cnblogs.com/over140/archive/2010/08/11/1792482.html

Preface

How can I publish the SQLite database and APK together? At the beginning, a friend told me to directly use the ADB command for manual release, or write SQL statements for dynamic creation, which is not ideal. Thanks to the implementation of the Android-based English dictionary by the galaxy messenger, this article has been supplemented and practiced by Article 2. Thank you for sharing it!

 

Article 

1. Implement an English dictionary based on Android

2. Using your own SQLite database in Android
Applications

3. for Android asset and Res/raw, the size of the uncompress file is limited to 1 MB.

 

Statement

You are welcome to repost, but please keep the original source of the article :)

Blog: http://www.cnblogs.com

Farmer's uncle: http://www.cnblogs.com/over140/

 

Body

I. Preparation

1.1 prepare a directory

Create a raw folder under Res of the android project. files in this folder will not be compiled and compressed.

1.2 prepare a database

Create or copy a database, open it, and execute the following two SQL statements:

Create Table "android_metadata" ("locale" text default 'zh _ cn') insert into "android_metadata" values ('zh _ cn ')

The purpose of this table is unclear. However, if you use the android SQLite API to create a database, this table will be taken by default. If you do not use this table, an error will be returned.

 

II. Implementation Code

/**
* Whether Initialization is complete
*/
Private Static Boolean isinit = false;


/**
* Initialize the database
* @ Param Context
*/
Synchronized public static void Init (context)
{
If (isinit)
Return;
// Output path
String outfilename = database_path + database_name;

// Check whether a file has been created
File dir = new file (outfilename );
If (dir. exists ())
Return;

// Check/create a database folder
Dir = new file (database_path );

If (! Dir. exists ())
Dir. mkdir ();

Inputstream input = NULL;
Outputstream output = NULL;
// Read the database stream from the Resource
Input = context. getresources (). openrawresource (R. Raw. DB );

Try {
Output = new fileoutputstream (outfilename );

// Copy to the output stream
Byte [] buffer = new byte [2048];
Int length;
While (length = input. Read (buffer)> 0 ){
Output. Write (buffer, 0, length );
}
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
// Close the output stream
Try {
Output. Flush ();
Output. Close ();
} Catch (ioexception e ){
}
// Close the input stream
Try {
Input. Close ();
} Catch (ioexception e ){
}
}
Isinit = true;
}

Code Description:

A). You can call it once in the main window (activity), and then use sqlitedatabase. opendatabase directly.

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.