During android development, sqlite with data is imported and installed on the mobile phone together with apk.

Source: Internet
Author: User

Sqlite is generally used in this way, that is, creating an empty database in the program, and then performing a series of operations such as adding, deleting, modifying, and querying, the development of small applications is easier to meet the requirements, but some large applications contain a large amount of data. If you insert data one by one in the program, first, it is time-consuming. Second, the user experience is poor. The most important thing is that it is not professional. Then, we can think of a better method, that is, how to install the db file and apk on your mobile phone. The implementation method is as follows:

First, you need to have the sqlite file containing data, which is db type. It can be imported one by one before, and then find the/data/package name/database file in the mobile phone path, export it;

Then, find the assets directory in the android project, put db-type files under the directory, and run the code after the database is ready.


[Java]
Public static SQLiteDatabase database;

Public static SQLiteDatabase database; [java] view plaincopyprint? String DB_PATH = "/data/com. example. mymap/databases /";
String DB_NAME = "wifi. db ";
 
 
// Check whether SQLite database files exist
If (new File (DB_PATH + DB_NAME). exists () = false ){
// If the SQLite database file does not exist, check whether the database directory exists.
File f = new File (DB_PATH );
// If the database directory does not exist, create the Directory
If (! F. exists ()){
F. mkdir ();
}
 
 
Try {
// Obtain the prepared SQLite database in the assets directory as the input stream.
InputStream is = getBaseContext (). getAssets (). open (DB_NAME );
// Output stream
OutputStream OS = new FileOutputStream (DB_PATH + DB_NAME );
 
 
// File writing
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 whether the database under/data/com. test. db/databases/works properly.
Database = SQLiteDatabase. openOrCreateDatabase (DB_PATH + DB_NAME, null );
Cursor cursor = database. rawQuery ("select * from wifi_private", null); // This statement fails
Log. v ("111", "111 ");
If (cursor. getCount ()> 0 ){
Cursor. moveToFirst ();
// Solve Chinese garbled characters
String SSID = cursor. getString (cursor. getColumnIndex ("SSID "));
// Byte test [] = cursor. getBlob (0 );
// String strtest = new String (test, "UTF-8"). trim ();
 
 
// Check whether the output information is correct
Log. v ("SSID", "" + SSID );
}
Cursor. close ();

Log. v ("222", "222 ");

String DB_PATH = "/data/com. example. mymap/databases /";
String DB_NAME = "wifi. db ";


// Check whether SQLite database files exist
If (new File (DB_PATH + DB_NAME). exists () = false ){
// If the SQLite database file does not exist, check whether the database directory exists.
File f = new File (DB_PATH );
// If the database directory does not exist, create the Directory
If (! F. exists ()){
F. mkdir ();
}


Try {
// Obtain the prepared SQLite database in the assets directory as the input stream.
InputStream is = getBaseContext (). getAssets (). open (DB_NAME );
// Output stream
OutputStream OS = new FileOutputStream (DB_PATH + DB_NAME );


// File writing
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 whether the database under/data/com. test. db/databases/works properly.
Database = SQLiteDatabase. openOrCreateDatabase (DB_PATH + DB_NAME, null );
Cursor cursor = database. rawQuery ("select * from wifi_private", null); // This statement fails
Log. v ("111", "111 ");
If (cursor. getCount ()> 0 ){
Cursor. moveToFirst ();
// Solve Chinese garbled characters
String SSID = cursor. getString (cursor. getColumnIndex ("SSID "));
// Byte test [] = cursor. getBlob (0 );
// String strtest = new String (test, "UTF-8"). trim ();


// Check whether the output information is correct
Log. v ("SSID", "" + SSID );
}
Cursor. close ();

Log. v ("222", "222 ");

 


In this way, the database is imported successfully. Note that the imported database does not conflict with the database created in your program (as long as the database name is different, table names are not the same.


 

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.