iOS常见错误1-SQLite错误-SQLITE_CANTOPEN,SQLITE_MISUSE和SQLITE_BUSY

iOS常见错误1-SQLite错误-SQLITE_CANTOPEN和SQLITE_MISUSE

在iOS开发中使用SQLite时候经常会发生一些错误,虽然都是很小的问题,但是不注意的话会耽误很长时间。

1、因为数据库路径错误造成SQLITE_CANTOPEN和SQLITE_MISUSE错误
//设置数据库的路径
-(NSString *)dataBasePath{
    //懒加载
    if (_dataBasePath==nil)
    {
    //这是正确的路径
    //NSString * documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
     //这是错误的路径,程序没有读写这个文件夹的权限,如果把数据库放在这里,就会造成错误
      NSString * documents=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)[0];
        
        
        
        self.dataBasePath=[documents stringByAppendingPathComponent:@"myDB.sqlite"];
        
        NSLog(@"myDB.sqlite路径=%@", _dataBasePath);
     }
    
    return _dataBasePath;
    
}

运行结果:因为程序没有此文件夹的权限,会造成打开数据库SQLITE_CANTOPEN错误,并且建表会造成SQLITE_MISUSE错误
2015-10-30 20:35:00.548 sqliteBusy[5102:502965] myDB.sqlite路径=/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/0C3A8481-EE08-42C7-B147-2CBE5B986DC4/Library/Documentation/myDB.sqlite
2015-10-30 20:35:00.550 sqliteBusy[5102:502965] myDB.sqlite数据库打开失败。错误代码=14
2015-10-30 20:35:00.550 sqliteBusy[5102:502965] UserInfo建表失败.错误代码=21
2015-10-30 20:35:00.550 sqliteBusy[5102:502965] myDB.sqlite数据库关闭成功


2、因为操作完数据库没有及时关闭,造成数据库BUSY错误。SQLITE_BUSY

//检查数据库中是否有某个用户
-(BOOL)checkUserWithName:(NSString*)name{
    
    BOOL checkOK=NO;
    
    [self openDB];
    
    NSString *sqlString=@"select m_id from  UserInfo where m_name = ?";
  
    sqlite3_stmt *stmt=NULL;
    
    int result = sqlite3_prepare(db, sqlString.UTF8String, -1, &stmt, NULL);
    
    if (result==SQLITE_OK)
    {
        
        NSLog(@"搜索预执行正确");
        
        sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
        
        while (sqlite3_step(stmt)==SQLITE_ROW)
        {
            //找到符合条件的记录
            checkOK = YES;
            return checkOK;
            //在这里返回,直接跳出方法,没有关闭数据库,会造成数据库错误
            
        }
        
    }
    else NSLog(@"预执行错误");
    
    
    sqlite3_finalize(stmt);
  
    [self closeDB];
    NSLog(@"关闭搜索伴随指针");
    return checkOK;
}


运行结果:因为操作数据库后没有关闭,再对数据库进行Delete,Update操作的时候会引发SQLITE_BUSY错误。
2015-10-30 21:37:26.341 sqliteBusy[5272:535359] myDB.sqlite数据库打开成功
2015-10-30 21:37:26.341 sqliteBusy[5272:535359] 删除用户预执行正确
2015-10-30 21:37:26.343 sqliteBusy[5272:535359] 删除用户失败。错误代码=5
2015-10-30 21:37:26.343 sqliteBusy[5272:535359] 删除用户关闭伴随指针
2015-10-30 21:37:26.343 sqliteBusy[5272:535359] myDB.sqlite数据库关闭成功


3、SQLite错误代码
 #define SQLITE_OK           0   /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR        1   /* SQL error or missing database */
#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
#define SQLITE_PERM         3   /* Access permission denied */
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
#define SQLITE_BUSY         5   /* The database file is locked */
#define SQLITE_LOCKED       6   /* A table in the database is locked */
#define SQLITE_NOMEM        7   /* A malloc() failed */
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL        13   /* Insertion failed because database is full */
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE_EMPTY       16   /* Database is empty */
#define SQLITE_SCHEMA      17   /* The database schema changed */
#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
#define SQLITE_MISMATCH    20   /* Data type mismatch */
#define SQLITE_MISUSE      21   /* Library used incorrectly */
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE_AUTH        23   /* Authorization denied */
#define SQLITE_FORMAT      24   /* Auxiliary database format error */
#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB      26   /* File opened that is not a database file */
#define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
/* end-of-error-codes */
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值