在花費數小時試圖解決這個問題之前,也許我可以使用某人的專業知識;是否可以在(網絡)服務器上生成SQLite數據庫並將其下載到Android設備,然後使用它?如何從Android設備下載SQLite數據庫?
我需要同步數據,但完全創建數據庫並將其作爲二進制文件發送可能會快得多。
在花費數小時試圖解決這個問題之前,也許我可以使用某人的專業知識;是否可以在(網絡)服務器上生成SQLite數據庫並將其下載到Android設備,然後使用它?如何從Android設備下載SQLite數據庫?
我需要同步數據,但完全創建數據庫並將其作爲二進制文件發送可能會快得多。
這裏是我的實現:
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();
}
其中是「copyFile」定義? – gonzobrains
對不起,添加它 – Blundell
getReadableDatabase沒有找到,你從哪裏得到它? – dvdciri
這個網站幫助了我一噸! http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
它教的很多東西是如何創建一個用於Android的數據庫(以及隨後的任何怪癖),並且還展示瞭如何將該代碼複製到應用程序內部數據庫存儲位置。
從那裏不應該很難弄清楚如何修改它來下載工作SQLLite
。
當我嘗試使用它下載文件時,您接受的解決方案似乎需要很長時間。我發現了一個更好的解決方案(帶有進度指示器)這裏:
http://www.hassanpur.com/blog/2011/04/android-development-downloading-a-file-from-the-web/
它包括源代碼和一步一步的教程。
「從該鏈接建立數據庫連接時出錯」 – athospy
我的答案是4年前給出的。也許試試Google的緩存。 – gonzobrains
是的..它是:-) – Blundell