Android database operations to directly read db files

Source: Internet
Author: User

There are two common methods for database operations:

1. Create a database and a table in the Code. 2. directly create the relevant database and table and copy the db file to the assets Directory. Now let's take a look at the second method: private String GetDataBasePath (Context context) {String packageName = context. getPackageName (); // Log. I ("PackName", packageName); // String DB_PATH = String. format ("/data/% 1 $ s/databases/", // packageName); String DB_PATH = CommonData. baseDir + File. separator + DB_NAME; if (new File (DB_PATH )). exists () = false) {try {// For example, SQLite database file Does not exist. Check whether the database directory contains File f = new File (DB_PATH); // if the database directory does not exist, create this directory if (! F. exists () {f. mkdir ();} // get the prepared SQLite database under the assets directory as the input stream InputStream is = context. getAssets (). open (DB_NAME); // output stream OutputStream OS = new FileOutputStream (DB_PATH); // write 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 () ;}return DB_PATH ;}

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.