如何从Android设备下载SQLite数据库? [英] How to download an SQLite database from an Android device?

查看:125
本文介绍了如何从Android设备下载SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在花费几个小时试图弄清楚这一点,也许我可以用别人的专长;才有可能产生一个(网络)服务器上的SQLite数据库,并将其下载到Android设备,然后使用它?

Before spending hours trying to figure this out, maybe I can use someone's expertise; would it be possible to generate an SQLite database on a (web)server and download it to an Android device and then use it?

我需要同步的数据,但它可能会更快完全创建数据库和发送二进制。

I need to sync data, but it probably would be much faster to create the database completely and send that as binary.

推荐答案

下面是我实现的:

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

此下载数据库到应用程序专用存储<一href="http://developer.android.com/reference/android/content/Context.html#openFileOutput%28java.lang.String,%20int%29">see这里

This downloads the database into your applications private storage see here

不要忘了你所需要的网络许可,您的清单。

Don't forget you need the internet permission in your manifest.

要然后得到一个实际的数据库从这个文件,你可以这样做:

To then get an actual database from this file you can do this:

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

这篇关于如何从Android设备下载SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆