2

我是 android 新手,我必须构建一个 android 应用程序,这将是一个测验,在这个我必须每次从给定表中选择随机问题。我已经建立了一个数据库,任何人都可以帮助我如何将它导入到我的应用程序。我将使用一个意图,它每次都调用相同的活动,并且每次在活动中调用一个随机编号。生成它会检查它是否更早生成,如果不是,那么它将从数据库中选择问题。

也欢迎任何其他有关从数据库中检索问题或实施此应用程序的建议。

4

3 回答 3

1

正如@rwilliams 所说,您可以将数据库从资产复制到应用程序目录..您只需要检查天气您是否已经复制了它..

这就是我的做法。

只需将您的数据库放在资产目录中。

public class DatabaseHelper extends SQLiteOpenHelper {

    public static String DB_PATH = "/data/data/com.aavid.advance.alarm.clock/databases/";
    private String dbName = "world_time.db";
    protected SQLiteDatabase theDatabase; 
    private final Context context;

    public DatabaseHelper(Context c, String dbName){
        super(c,dbName, null, 1);
        this.dbName = dbName;
        this.context = c;
    }

    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;
        try{
            String path = DB_PATH + dbName;
            checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
        }
        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException{

        File f = new File(DB_PATH);
        f.mkdirs();

        InputStream myInput = context.getAssets().open(dbName);
        String outFileName = DB_PATH + dbName;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() throws SQLException{
        String myPath = DB_PATH + dbName;
        theDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY|SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    }

    @Override
    public synchronized void close() {
        if(theDatabase != null)
            theDatabase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

}
于 2013-05-27T09:26:46.147 回答
0

Nasser 的回答是对的,但让我添加一些注释:

如果您更改数据库(添加字段,更改数据类型,...),

记住更改您的数据库文件名。

如果您不这样做,您的 APP 将不会读取最新的 DB 文件,直到您卸载 APP 并重新安装。

于 2013-05-27T09:32:12.040 回答
0

用户可以根据需要清除数据。所以你不能让你的数据库静态。但是,如果要保留数据库,可以将数据库复制到 assets 文件夹,当应用调用onCreateSQLiteHelper 的方法时,您将从 assets 文件夹复制到设备。

Def.DBNAME = "youdb.sqlite";

这是示例:

数据库助手

package vn.mve.db;

import java.util.List;

public interface DBHelper<T> {
    boolean insert(T val);
    boolean update(T val);
    boolean delete(T val);
    List<T> getList(int type);
    T getChild(Object val);
}

数据库处理程序

public class DBHandler extends SQLiteOpenHelper {
    private static final String TAG = DBHandler.class.getSimpleName();
    protected SQLiteDatabase db; 
    private final Context context;  
    private static String PACKAGE_NAME = "";
    private static int DATABASE_VERSION = 1;

    public DBHandler(Context context) {
        super(context, Def.DBNAME, null, DATABASE_VERSION);
        this.context = context; 
        PACKAGE_NAME = this.context.getPackageName();
        Def.FOLDER_DB = "/data/data/" + PACKAGE_NAME + "/databases/";
        Log.d(TAG, Def.FOLDER_DB);
        try {
            this.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            this.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        context.deleteDatabase(Def.DBNAME);
        onCreate(db);
    }

    public void createDataBase() throws IOException{
        // for first database;
        boolean dbExist = checkDataBase();
        if(!dbExist){
            try {
                copyDataBase("db/" + Def.DBNAME);
            } catch (Exception e) {
                Log.e(TAG, "createDatabse -> Copy failed!");
                throw new Error("Error copying database");
            }
        } else {
            open();
            boolean isExist = false;
            Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = 'config'", null);
            if (cursor != null) {
                isExist = true;
                cursor.close();
            } else {
                isExist = false;
            }
            close();
            Log.d(TAG, isExist + "");
            if (!isExist) {
                this.context.deleteDatabase(Def.DBNAME);
                try {
                    Log.d(TAG, "createDatabase when database has existed");
                    copyDataBase(Def.DBNAME);
                } catch (Exception e) {
                    Log.e(TAG, "createDatabse -> Copy failed!");
                    throw new Error("Error copying database");
                }               
            }
        }
    }   
    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase(String DB) {
        //Open your local db as the input stream
        InputStream myInput = null;
        //Open the empty db as the output stream
        OutputStream myOutput = null;
        try {
            myInput = context.getResources().getAssets().open(DB);

            // Path to the just created empty db
            String outFileName = Def.FOLDER_DB + Def.DBNAME; 
            myOutput = new FileOutputStream(outFileName);

            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0){
                myOutput.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "copyDatabase -> File not found.");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "copyDatabase");
        } finally {
              //Close the streams
            try {
                myOutput.flush();
                myOutput.close();
                myInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }   
    private boolean checkDataBase(){
        boolean checkDB = false;
        try{
            String myPath = Def.FOLDER_DB + Def.DBNAME;
            File dbFile = new File(myPath); 
            checkDB = dbFile.isFile();
            Log.d(TAG, "checkDatabase: " + String.valueOf(checkDB));
            try {
                File fTmp = new File(Def.FOLDER_DB);
                if (!fTmp.exists()) {
                    fTmp.mkdir();
                }
            } catch (Exception e) {
                Log.e(TAG, "checkDatabase" + e.getMessage());
            }
        }catch(SQLiteException e){}
        return checkDB;
    }
    public void open() {
        try {
            String myPath = Def.FOLDER_DB + Def.DBNAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);          
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void close() {
            if(db != null)
                db.close();
            super.close();
    }           
    public SQLiteDatabase getSqlDb() {
        return db;
    }
    public void setSqlDb(SQLiteDatabase sqlDb) {
        this.db = sqlDb;
    }    
}

和这里:

public class MVideo extends DBHandler implements DBHelper<Video> {
    public static final String TAG = MVideo.class.getSimpleName();
    public MVideo(Context context) {
        super(context);
    }

    @Override
    public boolean insert(Video val) {
        open();
        ContentValues cValues = new ContentValues();
        cValues.put(Def.Video.ID, val.getId());
        cValues.put(Def.Video.TITLE, val.getTitle());
        cValues.put(Def.Video.THUMBNAIL, val.getThumbnail());
        cValues.put(Def.Video.DESCRIPTION, val.getDescription());
        cValues.put(Def.Video.ENGLISH, val.getEnglish());
        cValues.put(Def.Video.VIETNAMESE, val.getVietnamese());
        cValues.put(Def.Video.ISVIEW, val.getIsView());
        long result = db.insert(Def.Video.NAME, null, cValues);
        close();        
        return result > 0;
    }

    public boolean insertList(List<Video> list) {
        open();
        db.execSQL("BEGIN IMMEDIATE TRANSACTION");
        for (Video v : list) {
            db.execSQL(String.format("INSERT INTO " + Def.Video.NAME + " (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\") VALUES" + 
                    " (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")",
                    Def.Video.ID, Def.Video.TITLE, Def.Video.THUMBNAIL, Def.Video.DESCRIPTION, 
                    Def.Video.ENGLISH, Def.Video.VIETNAMESE, Def.Video.ISVIEW,
                    v.getId(), v.getTitle(), v.getThumbnail(), v.getDescription(), v.getEnglish(), v.getVietnamese(), v.getIsView() + ""));
            Log.d(TAG, "insertList -> " + v.toString());
        }
        db.execSQL("COMMIT TRANSACTION");
        close();        
        return true;
    }

    @Override
    public boolean update(Video val) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean delete(Video val) {
        open();
        db.delete(Def.Video.NAME, Def.Video.ID + "=?", new String[]{val.getId()});
        close();
        return false;
    }

    @Override
    public List<Video> getList(int type) {
        List<Video> list = new ArrayList<Video>();
        open();
        Cursor c = db.rawQuery(Def.Video.GET_ALL, null);
        if (c.moveToFirst()) {
            while (c.moveToNext()) {
                String ID = c.getString(0);
                String title = c.getString(1);
                String thumbnail = c.getString(2);
                String description = c.getString(3);
                String english = c.getString(4);
                String vietnamese = c.getString(5);
                boolean isView = Boolean.parseBoolean(c.getString(6));
                list.add(new Video(ID, title, thumbnail, description, english, vietnamese, isView));
            }
        }
        close();
        return list;
    }

    @Override
    public Video getChild(Object val) {
        open();
        Cursor c = db.query(Def.Video.NAME, new String[]{
                Def.Video.ID, Def.Video.TITLE, Def.Video.THUMBNAIL, Def.Video.DESCRIPTION, 
                Def.Video.ENGLISH, Def.Video.VIETNAMESE, Def.Video.ISVIEW
        }, Def.Video.ID + "=?", new String[]{val.toString()}, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        Video v = new Video(c.getString(0), c.getString(1), 
                c.getString(2), c.getString(3), c.getString(4), 
                c.getString(5), Boolean.parseBoolean(c.getString(6)));
        close();
        return v;
    } 
}
于 2013-05-27T09:32:40.917 回答