简体   繁体   English

检查数据库SQLite Android的完整性

[英]Check integrity of Database SQLite Android

I have a SQLite database file in my server, and from time to time my Android App checks if there is a new SQLite database file. 我的服务器中有一个SQLite数据库文件,我的Android App会不时检查是否有新的SQLite数据库文件。 If true the App downloads the File and replaces the old database. 如果为true,则应用程序将下载文件并替换旧数据库。

The problem is, that some times the new database file gets corrupted and the App start to crashing and never recovers if I dont manualy clean the app in the Android Settings. 问题是,如果我不手动在“ Android设置”中清理应用程序,有时新的数据库文件会损坏并且应用程序开始崩溃,并且永远无法恢复。

My question is, there is a way to check the integrity of SQLite Database after the Downloaded? 我的问题是,有没有办法在下载后检查SQLite数据库的完整性?

This is my code for download the new Database from the server this code is placed in an AssyncTask : 这是我的代码,用于从服务器上下载新数据库,并将此代码放置在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;
    }

Look into: 调查:

pragma integrity_check;

it will scan the Database and check it for errors and other things too. 它将扫描数据库并检查其是否有错误以及其他情况。 More info(and more commands) can be found at this link: 在此链接中可以找到更多信息(和更多命令):

http://www.sqlite.org/pragma.html http://www.sqlite.org/pragma.html

also check out the documentation of isDatabaseIntegrityOk() . 还可以查看isDatabaseIntegrityOk()的文档。

You could try to use PRAGMA integrity_check (or Android's equivalent isDatabaseIntegrityOk() ), but this checks only the database structure for errors, and can detect only errors where it can prove that the structure is wrong. 您可以尝试使用PRAGMA integrity_check (或Android等效的isDatabaseIntegrityOk() ),但这只会检查数据库结构是否存在错误,并且只能在可以证明结构错误的地方检测到错误。

To be able to detect all errors (especially in your own data), you need to compute a checksum for the entire database file. 为了能够检测所有错误(特别是在您自己的数据中),您需要为整个数据库文件计算一个校验和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM