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.