SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
if (sql_ret != SQLITE_OK) {
ERR("Failed to open badge db[%d]", sql_ret);
- return BADGE_ERROR_FROM_DB;
+
+ if (sqlite3_errcode(db) == SQLITE_CORRUPT) {
+ if (db)
+ sqlite3_close(db);
+ unlink(BADGE_DB_PATH);
+
+ sql_ret = sqlite3_open_v2(BADGE_DB_PATH, &db,
+ SQLITE_OPEN_CREATE |SQLITE_OPEN_READWRITE,
+ NULL);
+ if (sql_ret != SQLITE_OK) {
+ ERR("Failed to open badge db[%d]", sql_ret);
+ unlink(BADGE_DB_PATH);
+ ret = BADGE_ERROR_FROM_DB;
+ goto out;
+ }
+ } else {
+ ret = BADGE_ERROR_FROM_DB;
+ goto out;
+ }
}
sql_ret = sqlite3_exec(db, CREATE_BADGE_TABLE, NULL, NULL, &errmsg);
if (sql_ret != SQLITE_OK) {
ERR("Failed to exec query[%d][%s]", sql_ret, errmsg);
- ret = BADGE_ERROR_FROM_DB;
+
+ if (sql_ret == SQLITE_CORRUPT || sql_ret == SQLITE_NOTADB) {
+ sqlite3_close(db);
+ unlink(BADGE_DB_PATH);
+
+ sql_ret = sqlite3_open_v2(BADGE_DB_PATH, &db,
+ SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
+ NULL);
+ if (sql_ret != SQLITE_OK) {
+ ERR("Failed to open db[%d]", sql_ret);
+ unlink(BADGE_DB_PATH);
+ ret = BADGE_ERROR_FROM_DB;
+ goto out;
+ }
+
+ sqlite3_free(errmsg);
+ sql_ret = sqlite3_exec(db, CREATE_BADGE_TABLE, NULL,
+ NULL, &errmsg);
+ if (sql_ret != SQLITE_OK) {
+ ERR("Failed to exec sqlite, again[%d][%s]",
+ sql_ret, errmsg);
+ unlink(BADGE_DB_PATH);
+ ret = BADGE_ERROR_FROM_DB;
+ goto out;
+ }
+ } else {
+ ret = BADGE_ERROR_FROM_DB;
+ goto out;
+ }
}
+out:
if (errmsg)
sqlite3_free(errmsg);