2012-11-29 7 views
2

我正在開發應用程序的Android應用程序中,我將數據庫從RES文件夾複製到本地數據庫,爲應用程序創建SQLite數據庫。在HTC Google Nexus One設備以外的其他設備和所有操作系統版本中均可正常工作。 我在第二次問這個問題,因爲我試圖以不同的方式發現解決方案。 我正在使用下面的代碼將數據庫複製到本地數據庫。在Android的Google Nexus One中將數據庫從RES文件夾複製到本地數據庫?

public class ECatalogueDatabase { 

    private static final String DB_PATH = "/data/data/com.weg.ecatalogue/databases/"; 
    public static final String DATABASE_NAME = "ECatalogue"; 
    public static final String DATABASE_TABLE = "T_Electrical"; 
    public static final int DATABASE_VERSION = 1; 

    public static final String KEY_ROWID="id"; 
    public static final String KEY_PRODUCT_LINE="productline"; 

    public static final String KEY_VOLTAGE="voltage"; 
    public static final String KEY_OUTPUTHP="outputhp"; 

    public static final String KEY_FRAME="frame"; 
    public static final String KEY_RPM="rpm"; 

    private Context context=null; 
    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    private static final String CREAT_DATABASE="Create Table if not exists "+ DATABASE_TABLE+"("+ KEY_ROWID +" INTEGER PRIMARY KEY NOT NULL," 
    +KEY_PRODUCT_LINE +" nvarchar ,"+ KEY_OUTPUTHP+" numeric ,"+ KEY_RPM +" nvarchar ,"+KEY_VOLTAGE +" nvarchar ," +KEY_FRAME +" nvarchar"+")"; 

    /** 
    * Constructor - takes the context to allow the database to be 
    * opened/created 
    * 
    * @param ctx the Context within which to work 
    */ 
    public ECatalogueDatabase(Context ctx) { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 
    //Helper class 
    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     public DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(CREAT_DATABASE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXISTS titles"); 
      onCreate(db); 
     } 

    } 

    public ECatalogueDatabase open() //throws SQLException 
    { 
     try 
     { 
      db=DBHelper.getWritableDatabase(); 

     }catch(Exception exception) 
     { 
      exception.printStackTrace(); 
     } 
     return null; 
    } 


    public void close() 
    { 
     DBHelper.close(); 
    } 

    /** 
    * Creates a empty database on the system and rewrites it with your own database. 
    * */ 
    public void createDataBase() throws IOException{ 

     @SuppressWarnings("unused") 
     boolean dbExist = checkDataBase(); 

     /** 
     * CHANGES DONE BY SHAILESH SHARMA TO SOLVE THE PROBLEM OF 2.2 HTC DESIRE IN CLIENT'S WIFE DEVICE :(
     */ 
     SQLiteDatabase db_Read = null; 
     if(dbExist){ 
     //DO NOTHING IN THIS CASE 
     }else{ 

     db_Read = DBHelper.getReadableDatabase(); 
     db_Read.close(); 
     } 
     //================================= 

     try { 

      copyDataBase(); 

     } catch (IOException e) { 

      throw new Error("Error copying database"); 

     } 

    } 
    /** 
    * Check if the database already exist to avoid re-copying the file each time you open the application. 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDataBase(){ 
     try{ 
      String myPath = DB_PATH + DATABASE_NAME; 
      //db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
      db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY); 

     }catch(SQLiteException e){ 

     } 

     if(db != null){ 

      db.close(); 

     } 

     return db != null ? true : false; 
    } 
    /** 
    * 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() throws IOException{ 


     InputStream myInput = context.getAssets().open(DATABASE_NAME); 

     // Path to the just created empty db 
     String outFileName = DB_PATH + DATABASE_NAME; 


     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 int getDatabaseCount(){ 
     int count = 0; 
     Cursor cursor = db.rawQuery("Select * from " + DATABASE_TABLE, null); 
     if(cursor!=null){ 
      count = cursor.getCount(); 
     } 
     cursor.deactivate(); 
     cursor.close(); 
     return count; 
    } 
} 
+0

http://stackoverflow.com/questions/9109438/how-to-use-an-現有的數據庫與Android應用程序/ 9109728#9109728 –

+0

你好Yaqub,謝謝你的回覆,但我也遵循相同的代碼,這兩個代碼沒有太大的區別... –

+0

檢查此示例項目,它應該工作:http://sdrv.ms/N857Wn –

回答

2

我已經得到了解決這個問題的方法,並且在這一刻我只是想和大家分享一下。 我是越來越

CREATE TABLE android_metadata failed 
Failed to setLocale() when constructing, closing the database 
android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed 

例外這是我在像HTC的Nexus One設備幾臺設備只獲得了完全不同的問題,這是在所有的操作系​​統和所有其他設備的工作。

當我們創建數據庫時,我們得到一個自動生成的名爲「android_table」的表,我刪除了該表並在我的SQLite管理器中手動重新創建了該表。通過以下兩個步驟查詢:

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US') 

INSERT INTO "android_metadata" VALUES ('en_US') 

當我運行的代碼這一步後,我在聖誕節前拿到了驚喜。我的應用程序現在工作正常。

一切歸功於我的長[R & d和這個環節,我經歷了這麼多的努力得到了:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ 
相關問題