The method of SQLite writing SD card in Android database _android

Source: Internet
Author: User
Tags sqlite

If the cell phone does not have root, the database file is unable to see, inconvenient debugging.

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

There are two places to modify:

1. In your helper class, database_name the name of the database file from the original filename and modifies it to the form of a path.

Before modification: database_name = "DEMO.DB"

public class Mydbhelper extends Sqliteopenhelper {public
static final int version = 1;//database version number public
static fin Al String database_name = "DEMO.DB"; Database name public
static FINAL String table_name = "mytag";//datasheet name, a database can contain more than one datasheet, similar to the Sheet1,sheet2 in Excel
// Mydbhelper's constructor, we are concerned with the name database_name and version versions public
Mydbhelper {
Super database_name, NULL, VERSION);

Modified: database_name = "/MNT/SDCARD/DEMO.DB"

public class Mydbhelper extends Sqliteopenhelper {public
static final int version = 1;//database version number public
static fin Al String database_name = "/MNT/SDCARD/DEMO.DB"; Database name public
static FINAL String table_name = "mytag";//datasheet name, a database can contain more than one datasheet, similar to the Sheet1,sheet2 in Excel
// Mydbhelper's constructor, we are concerned with the name database_name and version versions public
Mydbhelper {
Super database_name, NULL, VERSION);

Because if just a separate file name, the last created database file is stored in the phone's internal memory card (not running RAM, also not SD card) of the/data/data/package name/databases directory, and no root mobile phone, this/data root folder is not going to go, It can't be opened with the ADB shell.

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

Android phone is a strict security control, the SD card belongs to the external storage, access to the above files need to add permissions.

Add two SD card read and Write permissions to the Androidmanifest.xml:

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

If you do not add permissions, the program terminates abnormally.

The above mentioned is for the Android database SQLite write SD card method, hope to help everyone!

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.