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/" + 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 ");

}
}

 

 

From weizhiai12's column
 

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.