开发者

Using custom file for Android sqlite database

开发者 https://www.devze.com 2022-12-19 03:04 出处:网络
Is it possible to choose a custom location for the sqlite database file? If possible I want to store the database file in the memory card. And if user switches from one memory card to the other I 开

Is it possible to choose a custom location for the sqlite database file?

If possible I want to store the database file in the memory card. And if user switches from one memory card to the other I 开发者_运维知识库want my application to use whatever version of the database file available on the card.


By default your database is stored in data/data/your_package/databases

You can use SQLiteDatabase openOrCreateDatabase where you can supply the path to your custom database as your first argument.


You can access a database stored on you sdcard by using:

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class MyClass {
    private SQLiteDatabase myDB = null;

    // Constructor
    public MyClass() {
        try {
            myDB = SQLiteDatabase.openDatabase(stPathToDB, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Destructor
    public void finalize() {
        myDB.close();
    }
}

NO_LOCALIZED_COLLATORS = to open the database without support for localized collators.

In order to get the path to your sdcard you can use:

stPathToDB = android.os.Environment.getExternalStorageDirectory().toString()+"/dbase.sqlite"

Rgds Layne

0

精彩评论

暂无评论...
验证码 换一张
取 消