Android-create a database to the SD card

Source: Internet
Author: User

Android-create a database to the SD card
SQLite comes with SQLiteOpenHelper, while SQLiteOpenHelper stores the database to/data/package name/databasas, in this case, the SQLite database cannot be seen on a mobile phone without a root user. So, in another way, store the database SQLite on the SD card. The getWritableDatabase method is associated with this method. If (mName = null) {db = SQLiteDatabase. create (null);} else {db = mContext. openOrCreateDatabase (mName, 0, mFactory);} after analyzing the code above, it is found that when the database name is not empty, the database is created or opened by mContext. This mContext is passed in by the SQLiteOpenHelper constructor: SQLiteOpenHelper (Context context, String name, SQLiteDatabase. cursorFactory factory, int version ). Then we will reload the Input context Function openOrCreateDatabase so that it can create a database to the SD card to achieve our goal. Reload Context import java. io. file; import java. io. IOException; import android. content. context; import android. content. contextWrapper; import android. database. databaseErrorHandler; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. util. log; public class DatabaseContext extends ContextWrapper {public DatabaseContext (Context base) {super (Base);}/*** get the database path. If it does not exist, create the object * @ param name * @ param mode * @ param factory */@ Override public File getDatabasePath (String name) {// determine whether the SD card boolean sdExist = android exists. OS. environment. MEDIA_MOUNTED.equals (android. OS. environment. getExternalStorageState (); if (! SdExist) {// if it does not exist, Log. e ("SD card management:", "SD card does not exist, please load SD card"); return null;} else {// If yes // obtain the SD card path String dbDir = android. OS. environment. getExternalStorageDirectory (). getAbsolutePath (); dbDir + = "/database"; // database directory String dbPath = dbDir + "/" + name; // database path // determine whether the directory exists, if the directory does not exist, create File dirFile = new File (dbDir); if (! DirFile. exists () dirFile. mkdirs (); // whether the database File is successfully created boolean isFileCreateSuccess = false; // if the File exists and does not exist, create the File dbFile = new File (dbPath); if (! DbFile. exists () {try {isFileCreateSuccess = dbFile. createNewFile (); // Create File} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} else isFileCreateSuccess = true; // return the database file object if (isFileCreateSuccess) return dbFile; else return null;}/*** reload this method, is used to open the database on the SD card, android 2.3 and below will call this method. ** @ Param name * @ param mode * @ param factory */@ Override public SQLiteDatabase openOrCreateDatabase (String name, int mode, SQLiteDatabase. cursorFactory factory) {SQLiteDatabase result = SQLiteDatabase. openOrCreateDatabase (getDatabasePath (name), null); return result;}/*** Android 4.0 calls this method to obtain the database. ** @ See android. content. contextWrapper # openOrCreateDatabase (java. lang. string, int, * android. database. sqlite. SQLiteDatabase. cursorFactory, * android. database. principal) * @ param name * @ param mode * @ param factory * @ param errorHandler */@ Override public SQLiteDatabase openOrCreateDatabase (String name, int mode, CursorFactory factory, assumerrorhandler) {SQLiteDatabas E result = SQLiteDatabase. openOrCreateDatabase (getDatabasePath (name), null); return result ;}} call: DatabaseContext dbContext = new DatabaseContext (this); SdCardDBHelper dbHelper = new SdCardDBHelper (dbContext ); in particular, different android APIs call different openOrCreateDatabase functions. Of course, you can also directly use SQLiteDatabase to create a database on the SD card, or directly modify the source code of SQLiteOpenHelper to re-compile. However, the former does not perform some Test Fault Tolerance processing on the database, nor is it easier for SQLiteOpenHelper to operate. The latter has a large workload and is not recommended. Permission: <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission>

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.