将数据库从服务器复制到Android

and*_*edi 2 database android copy

我的网站空间有一个数据库.现在我想下载这个数据库并将其复制到我的/ databases文件夹中.它复制了数据库的某些部分但我无法访问它.使用"Root Explorer"时,它显示"Error - 打开数据库时出错.无法打开数据库文件"

private final static String DB_NAME = "werweisswasquiz";
private final static String DB_PATH = "data/data/my-package-name/databases/";


try {
        // Log.d(TAG, "downloading database");
        URL url = new URL("http://unnediger.bplaced.net/Files/mydbfile");
    URLConnection ucon = url.openConnection();
    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    ByteArrayBuffer baf = new ByteArrayBuffer(1024);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    /* Convert the Bytes read to a String. */
    OutputStream myOutput = new FileOutputStream(DB_PATH+DB_NAME);
    myOutput.write(baf.toByteArray());
    myOutput.flush();
    myOutput.close();
    bis.close();
    is.close();
} catch (Exception e) {
    Log.e("DOWNLOAD", "downloadDatabase Error: ", e);
    return false;
}
Run Code Online (Sandbox Code Playgroud)

and*_*edi 7

所以我终于找到了解决方案.

服务器认为文件"mydbfile"是一个txt文件,所以我为sqlite数据库添加了扩展名".db".现在它完美无缺.