【问题标题】:Android SQLite database for quiz game用于问答游戏的 Android SQLite 数据库
【发布时间】:2013-09-10 02:16:59
【问题描述】:

我正在制作一个问答游戏,我想使用 SQLite 数据库来存储 300 多个问题并随机选择其中一个问题。我做了一项研究,我知道如何创建和更新表,如何在 Android 应用程序中添加、修改和删除行,但我找不到将我的应用程序连接到已经完成的数据库(存储的只读数据库)的方法在资源(资产)文件夹中)。

你能帮帮我吗?

【问题讨论】:

  • 只是一个想法:当应用程序首次启动到应用程序的内部数据库时,解析文件(json/xml/whatever..)不是更好吗?您的资产文件必须是数据库文件吗?

标签: android sqlite


【解决方案1】:

您应该将 DB 文件从 assets 文件夹复制到已安装的应用程序中。
例如:

public class DatabaseOpenHelper extends SQLiteOpenHelper {

private final static String DB_NAME = "YourDatabaseFile.sqlite";
private static String DB_PATH = "/data/data/%s/databases/";
private final String ERROR_TAG = "error";
private final static int DB_VERSION = 1;
private final int BUFFER_SIZE = 8 * 1024;
private SQLiteDatabase databaseHandle;

private final Context context;

public DatabaseOpenHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);

    this.context = context;
    DB_PATH = String.format(DB_PATH, context.getPackageName());
}

public SQLiteDatabase openDataBase() {
    try {
        String databasePath = DB_PATH + DB_NAME;
        if (databaseHandle == null) {
            createDataBase();
            databaseHandle = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
        }
    }
    catch (SQLiteException e) {
        throw new IllegalStateException(context.getResources().getString(R.string.err_opening_db), e);
    }
    return databaseHandle;
}

private boolean createDataBase() {
    try {
        if (!isDataBase()) {
            this.getReadableDatabase();
            copyDataBase();
            return true;
        }
    }
    catch (SQLiteException e) {
        throw new IllegalStateException(context.getResources().getString(R.string.err_opening_db), e);
    }
    catch (IOException e){
        Log.e(ERROR_TAG, context.getResources().getString(R.string.err_close_stream), e);
    }

    return false;
}

public boolean isDataBase() {
    SQLiteDatabase verifiableDatabase = null;
    try {
        String databasePath = DB_PATH + DB_NAME;
        verifiableDatabase = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READONLY);
        verifiableDatabase.close();
    }
    catch (SQLiteException e) {
        Log.e(ERROR_TAG, context.getResources().getString(R.string.err_opening_db), e);
        return false;
    }

    return true;
}

private void copyDataBase() throws IOException {
    InputStream externalDbStream = null;
    OutputStream localDbStream = null;
    try {
        externalDbStream = context.getAssets().open(DB_NAME);
        localDbStream = new FileOutputStream(DB_PATH+DB_NAME);

        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead;

        while ((bytesRead = externalDbStream.read(buffer)) > 0) {
            localDbStream.write(buffer, 0, bytesRead);
        }
    }
    catch (IOException e) {
        throw new IllegalStateException(context.getResources().getString(R.string.err_copying_db), e);
    }
    finally {
        if (localDbStream != null)
            localDbStream.close();
        if (externalDbStream != null)
            externalDbStream.close();
    }
}

@Override
public void close() {
    databaseHandle.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

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

使用:
DatabaseOpenHelper dbHelper = new DatabaseOpenHelper(context);
SQLiteDatabase database = dbHelper.openDataBase();

【讨论】:

    【解决方案2】:

    这是一个非常复杂的事情,好吧不是很复杂,但你必须做一些编程的东西。无法直接从资产文件夹中读取数据库,您必须将其复制到内部存储或 sd 卡。在这里解释太多了,请阅读本教程,这是正确的起点:

    http://zaman91.wordpress.com/2010/09/22/android-how-to-use-own-sqlite-database/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多