jobernas
jobernas

Reputation: 694

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. If true the App downloads the File and replaces the old database.

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.

My question is, there is a way to check the integrity of SQLite Database after the Downloaded?

This is my code for download the new Database from the server this code is placed in an 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;
    }

Upvotes: 2

Views: 3404

Answers (2)

CL.
CL.

Reputation: 180080

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.

To be able to detect all errors (especially in your own data), you need to compute a checksum for the entire database file.

Upvotes: 0

Sagar Pilkhwal
Sagar Pilkhwal

Reputation: 3993

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

also check out the documentation of isDatabaseIntegrityOk().

Upvotes: 4

Related Questions