检查sqlite数据库完整性
2021/5/30 19:22:18
本文主要是介绍检查sqlite数据库完整性,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
最近遇到一个问题,用户数据丢失,拿到用户数据库文件以后,发现数据库损坏。
database disk image is malformed
因此希望可以找到一种方法,可以检测出来数据库是否损坏,经过google,找到了一种方法,先记录下来。
+ (BOOL)checkIntegrity { NSString *databasePath = [self databaseFilePath]; // File not exists = okay if ( ! [[NSFileManager defaultManager] fileExistsAtPath:databasePath] ) { return YES; } const char *filename = ( const char * )[databasePath cStringUsingEncoding:NSUTF8StringEncoding]; sqlite3 *database = NULL; if ( sqlite3_open( filename, &database ) != SQLITE_OK ) { sqlite3_close( database ); return NO; } BOOL integrityVerified = NO; sqlite3_stmt *integrity = NULL; if ( sqlite3_prepare_v2( database, "PRAGMA integrity_check;", -1, &integrity, NULL ) == SQLITE_OK ) { while ( sqlite3_step( integrity ) == SQLITE_ROW ) { const unsigned char *result = sqlite3_column_text( integrity, 0 ); if ( result && strcmp( ( const char * )result, (const char *)"ok" ) == 0 ) { integrityVerified = YES; break; } } sqlite3_finalize( integrity ); } sqlite3_close( database ); return integrityVerified; }
原链接
PRAGMA schema.integrity_check;
PRAGMA schema.integrity_check(N)This pragma does an integrity check of the entire database. The integrity_check pragma looks for out-of-order records, missing pages, malformed records, missing index entries, and UNIQUE and NOT NULL constraint errors. If the integrity_check pragma finds problems, strings are returned (as multiple rows with a single column per row) which describe the problems. Pragma integrity_check will return at most N errors before the analysis quits, with N defaulting to 100. If pragma integrity_check finds no errors, a single row with the value 'ok' is returned.
重点在这句话as multiple rows with a single column per row)
这样子,可以通过c代码来实现
根据文档,也可以使用quick_check来检查。
PRAGMA schema.quick_check;
PRAGMA schema.quick_check(N)The pragma is like integrity_check except that it does not verify UNIQUE and NOT NULL constraints and does not verify that index content matches table content. By skipping UNIQUE and NOT NULL and index consistency checks, quick_check is able to run much faster than integrity_check. Otherwise the two pragmas are the same.
区别在于integrity_check检查了
- out-of-order records(乱序的记录)
- missing pages(缺页)
- malformed records(错误的记录)
- missing index entries(丢失的索引)
- UNIQUE constraint(唯一性约束)
- NOT NULL (非空约束)
而且耗时较多。
quick_check不检查约束条件,耗时较短
这篇关于检查sqlite数据库完整性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2023-12-29"SQLite注入获取表名技巧"
- 2023-12-27SQLite数据库注入方式及安全性
- 2022-09-16Sqlite 并发读写的演进之路
- 2022-09-07修改ASQLite3D2010 支持中文显示
- 2022-09-0502-mORMot框架样例学习-02 - Embedded SQLite3 ORM(SQLite3 数据库)
- 2022-08-22基础复习——数据库SQLite——SQL的基本语法——数据库管理器SQLiteDatabase——数据库帮助器SQLiteOpenHelper
- 2022-08-21【数据库】SQLite数据库 工具
- 2022-08-06SQLite 错误:database disk image is malformed - 数据库磁盘映像格式不正确
- 2022-07-30学生信息管理系统(QT+SQLITE实现)
- 2022-07-22SQLite 建表、增删改查 简单操作