2011-08-23 16 views
9

Przed spędzeniem wielu godzin próbując to zrozumieć, może mógłbym skorzystać z czyjegoś doświadczenia; czy byłoby możliwe wygenerowanie bazy danych SQLite na serwerze (internetowym) i pobranie jej na urządzenie z systemem Android, a następnie użycie jej?Jak pobrać bazę danych SQLite z urządzenia z systemem Android?

Potrzebuję zsynchronizować dane, ale prawdopodobnie byłoby znacznie szybciej utworzyć bazę danych całkowicie i wysłać ją jako plik binarny.

+1

tak .. to jest :-) – Blundell

Odpowiedz

7

Oto moja realizacja:

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

ten pobiera z bazy danych w aplikacjach do prywatnego przechowywania see here

Nie zapomnij trzeba uprawnienie do Internetu w swoim manifeście.

Aby następnie uzyskać rzeczywistą bazę danych z tego pliku można to zrobić:

/** 
    * 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

gdzie jest zdefiniowane "copyFile"? – gonzobrains

+0

Przepraszamy, ale dodaliśmy – Blundell

+0

getReadableDatabase nie został znaleziony, skąd go masz? – dvdciri

4

Twoje zaakceptowane rozwiązanie zajęło dużo czasu, gdy próbowałem pobrać z niego plik. Znalazłem lepsze rozwiązanie (ze wskaźnikiem postępu) tutaj:

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

Zawiera kod źródłowy i krok po kroku tutorial.

+0

" Błąd podczas ustanawiania połączenia z bazą danych "z tego linku – athospy

+0

Moje odpowiedź została udzielona 4 lata temu. Może spróbuj pamięci podręcznej Google. – gonzobrains

Powiązane problemy