Direct read db file for Android database operation

Source: Internet
Author: User

There are two common ways to operate on a database:

1, build the library in the code, build the table; 2, directly set up the relevant libraries, tables, copy the DB file to the assets directory;   now look at the second method:  private String Getdatabasepath (Context Context) {    String PackageName = Context.getpackagename (); //log.i ("Packname", PackageName);  //String db_path = String.Format ("/data/data/%1$s/databases/", //PackageName);  string db_path = Comm Ondata.basedir + file.separator + db_name;   if ((New File (Db_path)). Exists () = = False) {   try {&nbsp ;  //If the SQLite database file does not exist, check to see if the databases directory exists     file F = new file (db_path);   //If the database directory is not stored In, new this directory     if (!f.exists ()) {     f.mkdir ();   }   //Get Assets directory we're ready for SQLite database as input stream     InputStream is = Context.getassets (). Open (db_name);   //output stream     OutputS Tream OS = new FileOutputStream (db_path);   //File write     byte[] buffer = new byte[1024];    I NT length;    while (length = Is.read (buffer)) > 0) {     os.write (buffer, 0, length);   }   //close file stream   &NB Sp Os.flush ();    os.close ();    is.close ();   } catch (Exception e) {    E.printsta Cktrace ();   } }   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.