I am currently writing a method that detects that database is corrupt or not
and return SQLITE_OK if OK otherwise an error.

If I detect an error then I delete the existing database file.and then
Create the new database file with the same name and after that populate the
database file with the default data or some known data. I know it is quite
bad approach.

The C language code of the method is like this.


int Verify_DB_integrity(sqlite3 *db_Handle)
{

   int  rVal = SQLITE_OK;
   char errMsg[400];
   sqlite3_stmt *pStmt;

   rVal = sqlite3_prepare(db_Handle, "PRAGMA integrity_check", sizeof(PRAGMA
integrity_check),
                                &pStmt, NU_NULL);

   if((rVal != SQLITE_OK) || (pStmt == NU_NULL))
   {
       return rVal;
   }

   rVal = sqlite3_step(pStmt);

   if (rVal == SQLITE_ROW)
   {

       strcpy(errMsg,(const char *) sqlite3_column_text(pStmt,0));

       rVal = sqlite3_finalize(pStmt);

       if((rVal != SQLITE_OK))
       {
           return rVal;
       }

       if (strcmp(errMsg,"ok") == 0)
       {
           return SQLITE_OK;
       }

       else
       {
           rVal  =   sqlite3_exec(Db_Handle, "VACUM",  0, NU_NULL, NU_NULL);
           if((rVal != SQLITE_OK) || (pStmt == NU_NULL))
           {
               return rVal;
           }

           rVal = sqlite3_prepare(Db_Handle, "PRAGMA integrity_check",
sizeof(PRAGMA integrity_check),
                                &pStmt, NU_NULL);
           if((rVal != SQLITE_OK) || (pStmt == NU_NULL))
           {
               return rVal;
           }

              rVal = sqlite3_step(pStmt);
              if (rVal == SQLITE_ROW)
              {
               strcpy(errMsg,(const char *) sqlite3_column_text(pStmt,0));

               rVal = sqlite3_finalize(pStmt);

               if((rVal != SQLITE_OK))
               {
                   return rVal;
               }

               if (strcmp(errMsg,"ok") == 0)
               {
                   return SQLITE_OK;
               }
           }

    }
    else
    {

       return rVal;
    }

    return rVal;

}


Now I have some questions.

1. Can you give me suggestions that above code is enough to check that
database is corrupt or not and if it is not enough then what statement or
functionality can be added so that  it could detect all the possible or
maximum corrupt scenarios.

2. When I add some extra characters at the end of  the database file though
some text editor and then call into that file "PRAGMA integrity_check"
statement. I get the error

     "SQLITE_NOTADB 26 /* File opened that is not a database file */".

Do you think that this error suggests that database file has been corrupt.
Should not this statement return OK and put the description of error in the
row of record.

3. As I am currently deleting the database file when I get the error from
the above method and I know it should be used as a last resort that's why I
also want to improve the above method.

But even if, after all the checking I get the error from the above method
and now I want to recover my database so what could be the best methods for
that.As far as know I can try
select statement on the corrupted database and if that statement
successfully executes then copy the records in to new database. I did not
try it though.

Thanks
Imran Habib
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to