Direct read db file for Android database operation

Source: Internet
Author: User

There are two common ways to operate on a database:
1, in the code to build a library, build a table;
2, directly set up the relevant libraries, tables, copy the DB file to the assets directory;

Now let's look at the second method:

Privatestring 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 = commondata.basedir + File.separator +db_name; if((NewFile (Db_path)). Exists () = =false) {   Try {    //if the SQLite database file does not exist, check to see if the databases directory existsFile f =NewFile (Db_path); //If the database directory does not exist, create a new directory    if(!f.exists ())    {F.mkdir (); }    //Get the Assets directory we are ready to use as an input stream for the SQLite databaseInputStream is=context.getassets (). open (db_name); //output StreamOutputStream OS =NewFileOutputStream (Db_path); //File Write    byte[] buffer =New byte[1024x768]; intlength;  while(length = is. Read (buffer)) >0) {os.write (buffer,0, length); }    //Close File StreamOs.flush ();    Os.close ();  is. Close (); } Catch(Exception e) {e.printstacktrace (); }  }   returnDb_path;}

Direct read db file for Android database operation

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.