2011-08-23 38 views
9

Trước khi dành hàng giờ cố gắng tìm ra điều này, có lẽ tôi có thể sử dụng chuyên môn của ai đó; nó sẽ có thể tạo ra một cơ sở dữ liệu SQLite trên một máy chủ (web) và tải nó xuống một thiết bị Android và sau đó sử dụng nó?Làm cách nào để tải xuống cơ sở dữ liệu SQLite từ thiết bị Android?

Tôi cần đồng bộ hóa dữ liệu, nhưng có thể sẽ nhanh hơn nhiều để tạo cơ sở dữ liệu hoàn toàn và gửi dưới dạng nhị phân.

+1

có .. nó .. là :-) – Blundell

Trả lời

7

Dưới đây là thực hiện của tôi:

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

này tải các cơ sở dữ liệu vào các ứng dụng của bạn lưu trữ tin see here

Đừng quên bạn cần sự cho phép internet trong biểu hiện của bạn.

Để sau đó nhận được một cơ sở dữ liệu thực tế từ tập tin này, bạn có thể làm điều này:

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

trong đó "copyFile" được xác định? – gonzobrains

+0

Xin lỗi, đã thêm nó – Blundell

+0

getReadableDatabase không được tìm thấy, bạn nhận được ở đâu? – dvdciri

1

Trang web này đã giúp tôi một tấn! http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

Khá nhiều những gì nó dạy là cách tạo cơ sở dữ liệu để sử dụng trong Android (và bất kỳ quirks nào theo sau) và nó cũng cho bạn biết cách sao chép mã đó vào vị trí lưu trữ cơ sở dữ liệu nội bộ của ứng dụng của bạn.

Từ đó không quá khó để tìm ra cách sửa đổi nó để hoạt động để tải xuống SQLLite.

4

Giải pháp được chấp nhận của bạn có vẻ mất nhiều thời gian khi tôi thử tải tệp xuống. Tôi tìm thấy một giải pháp tốt hơn (với chỉ số tiến bộ) ở đây:

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

Nó bao gồm mã nguồn và bước theo bước hướng dẫn.

+0

" Lỗi khi thiết lập kết nối cơ sở dữ liệu "từ liên kết – athospy

+0

của tôi câu trả lời đã được đưa ra cách đây 4 năm. Có thể thử bộ nhớ cache của Google. – gonzobrains

Các vấn đề liên quan