2013-07-20 66 views
0

我正在做一個問答遊戲,我想使用SQLite數據庫,我將存儲300多個問題並隨機選擇其中的一個。我做了一個研究,我知道如何創建和更新表格,如何添加,修改和刪除Android應用程序中的行,但我找不到一種方法將我的應用程序連接到已經完成的數據庫(只讀數據庫存儲在資源(資產)文件夾中)。用於問答遊戲的Android SQLite數據庫

你能幫我嗎?

+0

只是一個想法:那豈不是更好地解析文件(JSON/XML /任何..)當應用程序第一次啓動到應用程序的內部數據庫?您的資產文件是否必須是數據庫文件? – iBecar

回答

0

你應該從資產文件夾你的數據庫文件複製到安裝的應用程序。
例如:

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