0

我在我的应用程序中为 fatch 数据库实现了此代码,但它仅支持 2.3 及以上版本,但不支持 2.1、2.2 版本 请告诉我我的代码中是否写入的问题

public class Help extends SQLiteOpenHelper {

    private static String DB_PATH ="/data/data/com.emobi.metro/databases/";
    private static String DB_NAME ="mymetro";
    private final Context myContext;
    private static final int DATABASE_VERSION = 1;

public Help(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);


    this.myContext = context;
}

    public void createDataBase(){
        boolean dbExist = checkDataBase();
        if(dbExist) {
            Log.e("test","createDatabase[] - db exists");
            this.getWritableDatabase();
        }else{
            Log.e("test","createDatabase[] - db not exists");
            this.getReadableDatabase();
            try {
                copyDataBase();
                Log.e("test","createDatabase[] - copied db");
            }catch (IOException e){
                Log.e("test","createDatabase[] - copied not db");
            }
            SQLiteDatabase checkDB = null;
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);


        }
    }



    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
            Log.e("test","opened table");
        }catch(SQLiteException e){
            Log.e("test","didn't open table - not exists");
        }
        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }



    private void copyDataBase() throws IOException,SQLiteException{
        Log.e("tofu","copy database");
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[myInput.available()];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() throws SQLiteException, IOException{
        this.createDataBase();
        try { 
            String myPath = DB_PATH + DB_NAME;
            SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
        }catch(SQLException sqle){
            throw sqle;
        }
    }

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.e("dbUpgrade","OLD Version = "+oldVersion+"");
    Log.e("dbUpgrade","New Version = "+newVersion+"");
    myContext.deleteDatabase(DB_NAME);
    Log.e("dbUpgrade","deleted"+DB_NAME);
}

}

谢谢,

4

1 回答 1

0

只需将资产文件夹中的数据库文件 .db 扩展名更改为 .png 或任何其他可在 2.1、2.2 及更高版本设备中使用的压缩格式

于 2013-02-27T12:04:10.380 回答