我在我的服務器中有一個SQLite數據庫文件,我的Android應用程序不時檢查是否有新的SQLite數據庫文件。如果該屬性爲真,則應用程序將下載該文件並替換舊數據庫。檢查數據庫SQLite的完整性Android
問題是,有些時候新的數據庫文件被破壞,應用程序開始崩潰,並且如果我不手動清除Android設置中的應用程序,則無法恢復。
我的問題是,有一種方法來檢查下載後SQLite數據庫的完整性?
這是我下載的代碼從該代碼放置在一個AssyncTask服務器的新數據庫:
protected Boolean doInBackground(String... Url) {
try {
URL url = null;
if(Url[0].equals("")){
mSyncDate = mConnectionManager.getSyncDate();
url = new URL(Constants.HF_SERVER_DATABASE+"db_fxbus_"+convertDateToFormatYYYYMMDD(mSyncDate.getServerDate())+".sqlite");
}else{
url = new URL(Url[0]);
}
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
mDB.getReadableDatabase();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
Log.i(TAG, "Path:"+mContext.getDatabasePath("HorariosDoFunchal").getAbsolutePath());
OutputStream output = new FileOutputStream(mContext.getDatabasePath("HorariosDoFunchal").getAbsolutePath());
startWriting = true;
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
//Log.i(TAG, "Executing ...");
}
//Log.i(TAG, "Finish ...");
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e(TAG, e.toString());
return false;
}
return true;
}
您可以在服務器上創建一個MD5散列,並沿着新數據庫發送它,然後使用您下載的數據庫檢查它。看看[這篇文章](http://stackoverflow.com/q/5404925/3326331)它談論相同的數據庫完整性問題 – 2014-09-10 19:18:33
爲什麼你下載一個腳本,可以修改手機上的數據庫。 IE更改表添加列,更新,插入,刪除等。該腳本也可以用@SagarPilkhwal鏈接指出的查詢已知結果來測試自己。 – danny117 2014-09-10 19:25:38
我知道,我知道,但有時數據庫需要很長時間才能執行許多更改以更新和刪除並插入修改。對服務器來說這樣做要快得多。除了保存到文件中,應用程序會不時檢查更新。 – Bernas 2014-09-10 19:53:36