【问题标题】:How to download an SQLite database from an Android device?如何从 Android 设备下载 SQLite 数据库?
【发布时间】:2011-08-23 13:59:50
【问题描述】:

在花费数小时试图弄清楚这一点之前,也许我可以利用某人的专业知识;是否可以在(网络)服务器上生成 SQLite 数据库并将其下载到 Android 设备然后使用它?

我需要同步数据,但完全创建数据库并将其作为二进制发送可能会快得多。

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    这是我的实现:

         private static boolean downloadDatabase(Context context) {
                    try {
                           // Log.d(TAG, "downloading database");
                            URL url = new URL("http://some-url.com/db/" + "db_name.s3db");
                            /* Open a connection to that URL. */
                            URLConnection ucon = url.openConnection();
                            /*
                             * Define InputStreams to read from the URLConnection.
                             */
                            InputStream is = ucon.getInputStream();
                            BufferedInputStream bis = new BufferedInputStream(is);
                            /*
                             * Read bytes to the Buffer until there is nothing more to read(-1).
                             */
                            ByteArrayBuffer baf = new ByteArrayBuffer(50);
                            int current = 0;
                            while ((current = bis.read()) != -1) {
                                    baf.append((byte) current);
                            }
    
                            /* Convert the Bytes read to a String. */
                            FileOutputStream fos = null;
                            // Select storage location
                            fos = context.openFileOutput("db_name.s3db", Context.MODE_PRIVATE); 
    
                            fos.write(baf.toByteArray());
                            fos.close();
                           // Log.d(TAG, "downloaded");
                    } catch (IOException e) {
                            Log.e(TAG, "downloadDatabase Error: " , e);
                            return false;
                    }  catch (NullPointerException e) {
                            Log.e(TAG, "downloadDatabase Error: " , e);
                            return false;
                    } catch (Exception e){
                            Log.e(TAG, "downloadDatabase Error: " , e);
                            return false;
                    }
                    return true;
            }
    

    这会将数据库下载到您的应用程序私有存储see here

    别忘了你的清单中需要互联网权限。

    要从此文件中获取实际数据库,您可以这样做:

       /**
         * Copies your database from your local downloaded database that is copied from the server 
         * into the just created empty database in the
         * system folder, from where it can be accessed and handled.
         * This is done by transfering bytestream.
         * */
            private void copyServerDatabase() {
                // by calling this line an empty database will be created into the default system path
                // of this app - we will then overwrite this with the database from the server
                SQLiteDatabase db = getReadableDatabase();
                db.close();
    
    
                    OutputStream os = null;
                    InputStream is = null;
                    try {
                          // Log.d(TAG, "Copying DB from server version into app");
                            is = mContext.openFileInput("db_name.s3db");
                            os = new FileOutputStream("/data/data/your.package.name/databases/"); // XXX change this
    
                            copyFile(os, is);
                    } catch (Exception e) {
                            Log.e(TAG, "Server Database was not found - did it download correctly?", e);                          
                    } finally {
                            try {
                                    //Close the streams
                                    if(os != null){
                                            os.close();
                                    }
                                    if(is != null){
                                            is.close();
                                    }
                            } catch (IOException e) {
                                    Log.e(TAG, "failed to close databases");
                            }
                    }
                      // Log.d(TAG, "Done Copying DB from server");
            }
    
    
    
    
         private void copyFile(OutputStream os, InputStream is) throws IOException {
                byte[] buffer = new byte[1024];
                int length;
                while((length = is.read(buffer))>0){
                        os.write(buffer, 0, length);
                }
                os.flush();
        }
    

    【讨论】:

    【解决方案2】:

    当我尝试使用它下载文件时,您接受的解决方案似乎需要很长时间。我在这里找到了更好的解决方案(带有进度指示器):

    http://www.hassanpur.com/blog/2011/04/android-development-downloading-a-file-from-the-web/

    它包括源代码和分步教程。

    【讨论】:

    • 来自该链接的“建立数据库连接时出错”
    • 我的答案是 4 年前给出的。也许试试谷歌的缓存。
    • 请在您的回答中解释页面解释的内容。在链接失效的情况下,答案不得过时。
    【解决方案3】:

    这个网站帮了我很多忙! http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

    它所教的几乎是如何创建一个用于 Android 的数据库(以及随后的任何怪癖),它还向您展示了如何将该代码复制到您的应用程序内部数据库存储位置。

    从那里找出如何修改它以下载SQLLite应该不难。

    【讨论】:

    • 请在您的回答中解释页面解释的内容。在链接失效的情况下,答案不得过时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多