问题描述
我想提出一个问答游戏,我想使用SQLite数据库在那里我将存储300+问题,并随机选择其中之一。我做了一个研究,我知道如何创建和更新表,如何在Android应用程序添加,修改和删除行,但我不能找到一种方法,我的应用程序连接到已完成的数据库(存储在只读数据库在资源(资产)文件夹)。
I am making a quiz game and I would like to use SQLite database where I will store 300+ questions and randomly choose one of them. I made a research and I know how to create and update table, how to add, modify and delete rows within an Android app but I couldn't find a way to connect my app to an already done database (a read-only database stored in the resources(assets) folder).
你能帮帮我吗?
推荐答案
您应该从资产的文件夹中的数据库文件复制到安装的应用程序。
例如:
You should copy your DB file from assets folder to your installed application.
for example:
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 =新DatabaseOpenHelper(上下文);
SQLiteDatabase数据库= dbHelper.openDataBase();
Using:DatabaseOpenHelper dbHelper = new DatabaseOpenHelper(context);
SQLiteDatabase database = dbHelper.openDataBase();
这篇关于Android的SQLite数据库的问答游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!