检查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数据库完整性的更多相关文章
- Android在API推荐的方式来实现SQLite数据库的增长、删除、变化、检查操作
package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import android. ...
- Android之SQLite数据库篇
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...
- Android中SQLite数据库小计
2016-03-16 Android数据库支持 本文节选并翻译<Enterprise Android - Programing Android Database Applications for ...
- SQLite数据库在多线程写锁文件的解决办法
参考了很多SQLITE数据库多线程的解决办法 我自己写了一个SQLITEHELPER 来解决这个问题 希望大家多多指教 调用的时候 SQLLiteDBHelper _SQLLiteDBHelper ...
- 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...
- Android 中 SQLite 数据库的查看
当 SQLite 数据库创建完成后,如何查看数据库的内容呢?如果直接使用 File Explorer 查看,最多只能看到 database 目录下出现了一个 BookStore.db 文件,Book ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- 3.3 SQLite数据库
1.使用嵌入式关系型SQLite数据库存储数据 轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用 ...
- SQLite数据库增删改查操作
一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串 ...
随机推荐
- C#基础01
ASP.net基础详情 1:Momo就是跨平台的一种.net,借助其Momo可以让其.net网站跑到Lumin和安卓机上面. 2:开发的网站具有安全,速度快,容易配置. 3:互联网开发[网站]和管理系 ...
- HashSet 与TreeSet和LinkedHashSet的区别
Set接口 Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false. Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就 ...
- LINQ to SQL语句(4)之Join
适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectMany(Sel ...
- [Asp.net 5] Configuration-新一代的配置文件(ConfigurationSource的多种实现)
关于配置文件的目录:[Asp.net 5] Configuration-新一代的配置文件 在前面我们介绍了,系统中用IConfigurationSource表示不同配置文件的来源,起到读取.设置.加载 ...
- 让Windows2008R2也能进入手柄设置(游戏控制器设置)
在Windows2008 R2系统中,插入XB360手柄后能自动完成驱动安装,在[设备和打印机]中也会出现手柄,但在上面右键→游戏控制器设置却没反应,什么都没打开,虽然不影响实际游戏,但总感觉有点堵. ...
- lua学习之table类型
关系表类型,这是一个很强大的类型.我们可以把这个类型看作是一个数组.只是 C语言的数组,只能用正整数来作索引: 在Lua中,你可以用任意类型的值来作数组的索引,但这个值不能是 nil.同样,在C语言中 ...
- matlab 调用dos命令和文件操作
第一.利用!直接调用,简单方便,可以带操作对象:!del A.bat 第二.调用system函数或者dos函数,既可以实现功能,又返回参数,能检查执行情况,方便后面程序的开发,推荐这个 [status ...
- C#编程总结(九)字符编码
C#编程总结(九)字符编码 相信大家一定遇到过乱码的问题,为什么会乱码呢?输出的数据怎么就跟输入的不一样呢? 最近在总结加密问题,也遇到了同样的困扰.所以今天来集中解决这个问题. 什么是字符? 字符是 ...
- spring笔记3 spring MVC的基础知识3
4,spring MVC的视图 Controller得到模型数据之后,通过视图解析器生成视图,渲染发送给用户,用户就看到了结果. 视图:view接口,来个源码查看:它由视图解析器实例化,是无状态的,所 ...
- 后缀名为properties,config和xml的文件内容读取
1.先建立文件(后缀名为properties和config) 2.读取类建立 public class Read{ public static Properties properties = new ...