Android数据存储在SD卡中

时间:2015-04-12 18:10:32

标签: android android-sqlite android-sdcard

我创建了存储在SD卡中的数据库。但似乎发生了一些错误。 这是代码createDatabase

public void createDatabase(){
    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/smsgarbage.db";
    sqlite = openOrCreateDatabase(path, MODE_PRIVATE, null);
    try {
        String query = "CREATE TABLE tblGarbage(id text PRIMARY KEY, prefixNum text)";
        sqlite.execSQL(query);
        Toast.makeText(MainActivity.this, "Oncreate dabase was created", Toast.LENGTH_LONG).show();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

在清单文件中,我填写了:

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

SomeOne谁掌握了这个问题,请花点时间再检查一下以支持我。我很欣赏这一点。

1 个答案:

答案 0 :(得分:0)

嗨,如果您想通过SD卡创建数据库正确的实施方式,请参阅以下方式,如果您在任何时候没有得到我,请告诉我您是否有任何疑问。

步骤1.使用完整的SD卡路径扩展SQLiteOpenHelper替换数据库名称。这将在定义位置创建数据库。

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


import com.adb.test.sql.DAO.FoodMarchantDAO;


public class SQLite extends SQLiteOpenHelper {

    public static int DATABASE_VERSION = 1;

    public static String DATABASE_NAME = "smsgarbage.db";

    private static final String DATABASE_PATH_NAME = getDBAbsolutePath();

    /**
     * Create a helper object to create, open, and/or manage a database. This method always returns very quickly. 
     * The database is not actually created or opened until one of getWritableDatabase()
     * or getReadableDatabase() is called.
     * @param context
     * @param name
     * @param factory
     * @param version
     */
    public SQLite(Context context) 
    {
        super(context, DATABASE_PATH_NAME, null, version);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // when we call SQLiteDatabase getWritableDatabase() this would called
        // In fist time application install if no database then it would called once but that time Application 
        // database might be null we have to used here db
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
    }

    private static String getDBAbsolutePath() {
        return new StringBuilder().append(appHomeDirectory()).append(DATABASE_NAME).toString();
    }

    public static String appHomeDirectory() {
        return new StringBuilder().append(getExternalStoragePath()).append("foodbazer").append("/").toString();
    }
}

步骤2.在应用程序级别,如何访问SQLite帮助类,请参阅下面的内容。

import com.adb.test.sql.data.SQLite;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;


public class FoodBazerApplication extends Application{

    private static FoodBazerApplication mFoodBazerApplication;

    public SQLiteDatabase mAppLevelDB;

    public static FoodBazerApplication getInstance()
    {
        return mFoodBazerApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mFoodBazerApplication = this;
        SQLite tmpSQL= new SQLite(getApplicationContext());
        // it return null if first time database create onCreate() called,
        // once database create it will open the database so openDataBase() called
        mAppLevelDB = tmpSQL.getWritableDatabase();
    }
}