2013-03-10 31 views
0

這是我的代碼。我使用「sqlite瀏覽器」創建了一個.db文件,並將其放置到我的工作空間的「資產」文件夾中。但'.db'文件沒有被複制到應用程序路徑中(當我在DDMS(data/data/com.example.trans /)數據庫文件夾沒有被創建時)。此外.db文件不從資產文件夾複製。無法打開真實設備中的db文件

這裏是我的代碼:

public class ConnectionFactory extends SQLiteOpenHelper{ 
    private static String DB_Path = "/data/data/com.example.translationapp/databases/"; 
    public static String DB_Name = "Translator.db"; 
    public SQLiteDatabase myDb; 
    File dbFile; 
    public Context con; 

    public ConnectionFactory(Context context) { 
     super(context, DB_Name, null,1); 

     // TODO Auto-generated constructor stub 
     this.con= context; 
    } 

    public void OpenDb() throws IOException 
    { 
     boolean dbCheck = checkDb(); 
     if(dbCheck) 
     { 

     } 
     else 
     { 
      this.getWritableDatabase(); 
      try 
      { 
       copyDatabase(); 
      } 
      catch(IOException e) 
      { 
       throw new Error("Sorry"); 
      } 
     } 
    } 
    public boolean checkDb() throws IOException 
    { 
     SQLiteDatabase checkDb =null; 
     try 
     { 
     String myPath = DB_Path+DB_Name; 
     checkDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
     } 
     catch(SQLiteException e) 
     { 

     } 
     if(checkDb!= null) 
     { 
      checkDb.close(); 
     } 
     return checkDb !=null ? true:false; 
    } 
    public void copyDatabase()throws IOException 
    { 
     try 
     { 
     File filetext = con.getFileStreamPath(DB_Name); 
     boolean exists = filetext.exists(); 
     if(!exists) 
     { 
     String outFileName = DB_Path+ DB_Name; 

     myDb=con.openOrCreateDatabase(outFileName, Context.MODE_PRIVATE, null); 
     OutputStream myOut = new FileOutputStream(filetext.getAbsolutePath()); 
     InputStream myin = con.getAssets().open(DB_Name); 
     byte[] buffer = new byte[1024]; 
      int length; 
      while((length = myin.read(buffer))>0) 
      { 
       myOut.write(buffer,0,length); 
      } 
      myOut.flush(); 
      myOut.close(); 
      myin.close(); 
     } 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 

    } 
    public void openDatabase() throws SQLException 
    { 
     String myPath = DB_Path+DB_Name; 
    myDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    } 
    public synchronized void close() 
    { 
     if(myDb != null) 
     { 
     myDb.close(); 
      super.close(); 
     } 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

你不會能夠這樣寫的,除非你的設備是植根 – DjHacktorReborn 2013-03-10 11:38:19

+0

@ DjHacktorReborn :請讓我如何rescarve這個問題programitically ..對不起,問plzzzzz – Raviteja 2013-03-10 11:41:37

+0

使用遊標運行查詢您的數據庫搜索存儲的表中,你會發現結果 – DjHacktorReborn 2013-03-10 11:42:54

回答

1

取而代之的資產文件夾硬編碼數據庫的路徑,只要使用此 -

private static String DB_Path = "/data/data/"+getPackageName()+"/databases"; 
+0

這在無根的devic中不起作用 – DjHacktorReborn 2013-03-10 12:00:08

+0

但是當資產的db文件沒有複製到DDMS時。我們如何期待這個查詢將被執行。主要是它拋出了一個錯誤,「無法打開databaase」(錯誤= 14) – Raviteja 2013-03-10 12:03:29

+0

@DjHacktorReborn DDMS模式不會在任何Android設備進來:-P – 2013-03-10 12:17:05