Android database SQLite writes SD card, androidsqlite

Source: Internet
Author: User

Android database SQLite writes SD card, androidsqlite

If the mobile phone does not have a root user, the database files cannot be viewed and debugging is not convenient.

The best way is to write the database into the SD card.

 

There are two changes:

1. In your helper class, change the database file name DATABASE_NAME from the original file name,To the path format.

Before modification: DATABASE_NAME = "demo. db"

Public class MyDBHelper extends SQLiteOpenHelper {public static final int VERSION = 1; // database VERSION number public static final String DATABASE_NAME = "demo. db "; // database name public static final String TABLE_NAME =" mytag "; // data table name. A database can contain multiple data tables, similar to Sheet 1 in excel, sheet2 // MyDBHelper constructor. We are concerned with the name DATABASE_NAME and VERSION public MyDBHelper (Context context) {super (context, DATABASE_NAME, null, VERSION );}

After modification: DATABASE_NAME = "/mnt/sdcard/demo. db"

Public class MyDBHelper extends SQLiteOpenHelper {public static final int VERSION = 1; // database VERSION number public static final String DATABASE_NAME = "/mnt/sdcard/demo. db "; // database name public static final String TABLE_NAME =" mytag "; // data table name. A database can contain multiple data tables, similar to Sheet 1 in excel, sheet2 // MyDBHelper constructor. We are concerned with the name DATABASE_NAME and VERSION public MyDBHelper (Context context) {super (context, DATABASE_NAME, null, VERSION );}

Because if it is just a separate file name, The Last database file created is saved on the internal memory card of the mobile phone (neither the memory nor the SD card) /data/package name/databases directory, without the root phone, this/data root folder cannot be entered, and cannot be opened in adb shell mode.

 

2. Finally, do not forget to modify the permission!

The Android mobile phone has strict security control. The SD card belongs to the external storage. You need to add permissions to access the above files.

Add two SD card read and write permissions to AndroidManifest. xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If no permission is added, the program will terminate unexpectedly.

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.