Android SQLite database for quiz game

前端 未结 2 727
一生所求
一生所求 2021-01-16 16:03

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

相关标签:
2条回答
  • 2021-01-16 16:44

    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) {
    }}
    

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

    0 讨论(0)
  • 2021-01-16 16:44

    This is a very complicated thing, ok not very complicated but You have to do some programming stuff. To read a database from asset folder is not directly possible, You have to copy it to the internal storage or to sd card. To explain it here is to much, please read this tutorial, that´s the right point to start:

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

    0 讨论(0)
提交回复
热议问题