0
我在我的應用程序中實現這個代碼爲fatch數據庫,但它只支持2.3版本及以上,但不支持2.1,2.2版本 請告訴我關於我的問題代碼是寫還是不sqlite數據庫不支持2.1,2.2版本但2.3支持
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);
}
}
感謝,
任何堆棧跟蹤?錯誤信息?你怎麼知道它不被支持? – 2012-07-25 07:40:05
您正在複製的數據庫的大小是多少?它是否超過1MB? – 2012-07-25 07:47:53
是的,我的數據庫超過1 MB,其大小是4.4MB – user1550819 2012-07-25 07:56:18