【问题标题】:Android data stored in sd card存储在 sd 卡中的 Android 数据
【发布时间】:2015-04-12 18:10:31
【问题描述】:

我已经创建了存储在 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"/>

有人对此问题有高手,请花点时间再次查看以支持我。我对此表示赞赏。

【问题讨论】:

  • something is wrong happened ... 发布错误日志。那么只能找到问题了。
  • 出了什么问题?

标签: android android-sqlite android-sdcard


【解决方案1】:

您好,如果您想通过 sdcard 以正确的方式创建数据库,请参阅以下方式,如果您有任何疑问,如果您在任何时候都没有找到我,请告诉我。

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

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();
    }
}

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多