2012-04-23 62 views
0

我已經在我的android應用程序中創建數據庫,然後插入一個語句。一切正常,所以我想讓我的數據庫來自MY_PACKAGE/databses /並將它複製到SD卡上以便到達。當打開sqlite得到一個異常android

這個工作,但是當我試圖用我的源碼的Firefox插件打開我得到這個錯誤:

SQLiteManager: Error in opening file Datas.sqlite - either the file is encrypted or corrupt 
Exception Name: NS_ERROR_FILE_CORRUPTED 
Exception Message: Component returned failure code: 0x8052000b (NS_ERROR_FILE_CORRUPTED) [mozIStorageService.openUnsharedDatabase] 

也許我有別的辦法打開或我無法打開該這麼容易?

我會給我使用的所有代碼:

處理我的數據庫,我用所有這些代碼:

Using your own SQLite database in Android applications

其複製到SD卡這種方法:

public static boolean backUpDataBase(Context context) { 

     final String DATABASE_NAME = "Data.sqlite"; 
     final String DATABASE_NAME_FULL = "/data/data/package/databases/" 
       + DATABASE_NAME; 
     boolean result = true; 

     // Source path in the application database folder 
     String appDbPath = DATABASE_NAME_FULL; 

     // Destination Path to the sdcard app folder 
     String sdFolder = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/" + "Datas.sqlite"; 

     File f = new File(sdFolder); 
     // if (!f.exists()) { 
     // f.mkdir(); 
     // } 

     InputStream myInput = null; 
     OutputStream myOutput = null; 
     try { 
      // Open your local db as the input stream 
      myInput = new FileInputStream(appDbPath); 
      // Open the empty db as the output stream 
      myOutput = new FileOutputStream(f); 

      // 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 (IOException e) { 
      result = false; 
      e.printStackTrace(); 
     } finally { 
      try { 
       // Close the streams 
       if (myOutput != null) { 
        myOutput.flush(); 
        myOutput.close(); 
       } 
       if (myInput != null) { 
        myInput.close(); 
       } 
      } catch (IOException e) { 
      } 
     } 

     return result; 
    } 

我的數據庫是這樣的: 2表格:

CREATE TABLE "Test" ("_id" INTEGER PRIMARY KEY NOT NULL UNIQUE , "Info" TEXT) 

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

和代碼來完成所有我需要的:這是讀寫 DataBaseHelper數據庫= Main.createOrOpenDB(mContext)

//返回DATABSE;

 Main.backUpDataBase(mContext); 

     db = dataBase.myDataBase; 

     // Step 1: Inflate layout 
     setContentView(R.layout.tabs_fragment_activity); 

     try{ 
     db.execSQL("INSERT INTO " +"Test" +" Values ('1','Inserted');"); 
     }catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 

那麼,哪裏是錯誤的,因爲插入工作正常?

回答

0

聽起來好像你的代碼有寫入SD卡的問題(我沒有立即看到)。

我想知道,爲什麼要將它複製到SDCard?這聽起來像你只是想檢查文件...

如果這實際上是你的目標,那麼我會建議運行模擬器,並簡單地使用eclipse中的DDMS視圖,導航到該文件,然後單擊上面的按鈕工具提示說「從設備中拉出文件」的右角。你在模擬器中得到的應該是你在手機上得到的。

相關問題