Load the database to the current package under data/data in Android

Source: Internet
Author: User

In the Android system, if you need to use a database, we generally use the sqllite database and store the database in the res \ raw directory. This facilitates the release of the database together when the system is released.

When accessing the database in the system, we generally copy the database to the directory of the name of our current package under data/data, which makes it easier for us to perform operations.

You can refer to the following sample code:

// Copy and load data in the regional database
Private void copyandloaddb (){
// When you run the application for the first time, load the database to data/Name of the current package/database/<db_name>
Dir = new file ("Data/data/" + getpackagename () + "/databases ");
If (! Dir. exists () |! Dir. isdirectory ()){
Dir. mkdir ();
}
File = new file (Dir, "china_province_city_zone.db3 ");
If (! File. exists ()){
Fileutil. loaddbfile (R. Raw. china_province_city_zone, file,
Getresources (), getpackagename ());
Log. D ("winestock", "database load successfully ");

}
}

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.