sqlite数据库不支持2.1,2.2版本但2.3支持

问题描述:

我在我的应用程序中实现这个代码为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); 
} 

}

感谢,

+0

任何堆栈跟踪?错误信息?你怎么知道它不被支持? – 2012-07-25 07:40:05

+0

您正在复制的数据库的大小是多少?它是否超过1MB? – 2012-07-25 07:47:53

+0

是的,我的数据库超过1 MB,其大小是4.4MB – user1550819 2012-07-25 07:56:18

只要改变在资产文件夹中的数据库文件.db扩展到png格式或其他压缩格式,将在2.1借机, 2.2及以上设备s