使用数据库为Android应用程序下载与php脚本


Use database for Android App by downloading with php-script

我要疯了!也许有人可以帮助我?!我在正在运行的服务器上有一个sqlite数据库,由于php脚本,我收到了该数据库。(为了清楚起见:我正在调用一个php脚本,它给了我数据库作为响应(。通过响应,我现在尝试将其"解析"为常规的 *.db 文件,稍后我将该文件用于我的应用程序。

该应用程序工作正常,同时将 *.db 放入资产文件夹中。但是每次调用应用程序时,我都需要获取更新的数据库。因此,我需要以某种方式从服务器接收它。

只是为了注意:我不知道他们为什么使用php脚本,但它与应用程序的iOS版本完美配合。所以我 100% 确定该脚本确实有效。

有任何提示或解决方案吗?谢谢!

编辑:这是我正在尝试做的事情。 private void copyDatabase(( { InputStream myInputDB = null; OutputStream myOutputDB = null; HttpResponse Response = null; 刚刚创建的空数据库的路径 字符串 dbFilePath = DB_PATH + KeyConstants.DB_NAME;

     // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost(
            "http://USERNAME:PASSWORD@ADRESS/u/db.php");

    try {
        response = httpClient.execute(httpPost);
        //Open your local db as the input stream
        myInputDB = response.getEntity().getContent();
        //Open the empty db as the output stream
        myOutputDB = new FileOutputStream(dbFilePath);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInputDB.read(buffer)) > 0){
            myOutputDB.write(buffer, 0, length);
        }
        //Close the streams
        myOutputDB.flush();
        myOutputDB.close();
        myInputDB.close();
    } catch (IOException ioEXC) {
        throw new Error("Problem copying database from resource file.");
    }

你有什么问题?使用 Android 应用程序还是使用 PHP 脚本?

我不明白为什么有一个完整的数据库文件请求-响应,而不仅仅是数据(在一些懒惰的阅读中更好(,但这不是你的情况。

如果您需要在每次运行时"下载"所有数据库,您应该调用并实现一些方法来执行此操作 - 在第一个活动中,在 onCreate(( 方法中调用此方法。您可以在此活动中创建一个简单的方法,或者以更好的方法为此创建一个简单的类,该类将被实例化,并在第一个活动的 onCreate(( 中调用其方法。

但也许我只是不明白你的问题...

编辑:尝试阅读这个问题:我可以在/sdcard 上下载 SQLite 数据库并从我的 Android 应用程序访问它吗?