如何从Android设备下载SQLite数据库?

11
在花费数小时试图解决这个问题之前,也许我可以利用某人的专业知识; 是否有可能在(Web)服务器上生成一个SQLite数据库,然后将其下载到Android设备并使用它?
我需要同步数据,但完全创建数据库并以二进制形式发送可能会更快。
3个回答

10

这是我的实现:

     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;
        }

这将把数据库下载到您应用程序的私有存储中(请看这里)

不要忘记在您的清单文件中添加网络权限。

要从该文件获取实际的数据库,可以执行以下操作:

   /**
     * 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();
    }

getReadableDatabase没有找到,你从哪里获取它? - dvdciri
假设是SQLiteOpenHelper的扩展,参见:https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase() - Blundell

2

"Error establishing a database connection" 的意思是“无法建立数据库连接”。 - athospy
我的回答是4年前给出的。也许可以尝试使用谷歌的缓存。 - gonzobrains
1
请在您的答案中解释页面的内容。如果链接失效,答案不应过时。 - LudvigH

-1

请在您的答案中解释页面所解释的内容。如果链接失效,答案不应过时。 - LudvigH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接