android - 如何在android中将数据库文件复制到sdcard

标签 android cordova

<分区>

Possible Duplicate:
How to backup database file to sdcard on android?

我正在开发 phonegap 1.3.0。我在 phonegap 中创建了名为 test.db 的数据库,该数据库存储在 data/data/(pakage name)/test.db 中。我正在使用 native android 代码将数据库复制到 sdcard (mnt/sdcard)。但是它给出了 test.db 不存在的错误。该设备未 Root 。我缺少什么?

提前致谢。

代码如下->

public void copyfile(String srFile, String dtFile) throws FileNotFoundException{
    String DBPATH = "/data/data/package_name/databases/";
     String DBNAME = "test.db";
     String myPath = DBPATH + DBNAME;

    //Open your local db as the input stream
    File externalStorage = Environment.getExternalStorageDirectory();

    InputStream myInput = new FileInputStream(myPath);

    // Path to the just created empty db
    String outFileName = "/mnt/sdcard/folder/test.db";

    //Open the empty db as the output stream
    OutputStream myOutput;
    try {
    myOutput = new FileOutputStream(outFileName);
    //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);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

phonegap代码如下:

<!DOCTYPE html>

<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>


 <script type="text/javascript" charset="utf-8">
 db = window.openDatabase("test", "1.0", "PhoneGap Demo", 200000);
function createDB() {
    db.transaction(populateDB);
}
function populateDB(tx) {
    Field17=document.form.Field17.value;
    Field18=document.form.Field18.value;
    Field19=document.form.Field19.value;
    //tx.executeSql('DROP TABLE IF EXISTS INFO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS INFO (pid integer primary key, field17,field18,field19');
    sql = "INSERT INTO INFO (field17,field18,field19) VALUES ( '" + Field17 + "','" + Field18 + "','" + Field19 + "')";
    tx.executeSql(sql);
    db.transaction(queryDB);
}


function queryDB(tx,results) {
    console.log("inside querydb");
    tx.executeSql('SELECT * FROM INFO', [], querySuccess);
}

function querySuccess(tx, results) {     
var len = results.rows.length;
    alert('rows inserted: ' + len);
    if (len > 0) {
        for (var i=0;i<len;i++) {

            console.log('pid: ' + results.rows.item(i).pid + 'field: ' + results.rows.item(i).field17 + ' field: ' + results.rows.item(i).field18+ ' field ' + results.rows.item(i).field19);

        }
    }
}       


</script>

最佳答案

您无需对手机进行 root。检查权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> .

可以查看文件

File dbFile = new File(DB_PATH + DB_NAME);
        if(dbFile.exists()){
                  //Continue

编辑:测试这段代码。更改 sdcard 中的 pakage 名称和 Foler

public boolean checkDataBase() {
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

public void copyfile(String srFile, String dtFile) throws FileNotFoundException{
    private static String DB_NAME = "dictionary.db";
    private static String DB_PATH = "/data/data/com.app.android.dictionary/databases/";
if(checkDataBase()){
InputStream myInput = new FileInputStream(DB_PATH + DB_NAME);

    // Path to the just created empty db
    String outFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/foler/" + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput;
    try {
    myOutput = new FileOutputStream(outFileName);
    //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);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

}
}

关于android - 如何在android中将数据库文件复制到sdcard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10005030/

相关文章:

javascript - ../plugins/cordova-plugin-file/www/FileWriter.js 中的无效字符“u0187”

android - 从另一个 Activity 更新 Activity

android - 以百分比显示进度对话框

javascript - 将数据发布到 Cordova 中的 REST 服务和内部服务器错误

android - 如何在 Kotlin 1.1.60 中使用@Parcelize?

cordova - 未定义AudioToggle

javascript - jquery ajax POST 中出现 400(错误请求)错误

javascript - phonegap window.location 不适用于 .html#page

android - Android 中的 JSON 数组拆分器

android - Xtype 文本字段上的 Sencha 触摸键盘下一个按钮