sqlite数据库工作正常,突然显示无法打开数据库错误

时间:2015-04-07 11:45:05

标签: ios sqlite ios8

您好我正在创建一个包含Sqlite的数据库应用程序,一切正常并突然发现20-30个事务数据库未找到错误,请帮帮我

-(void)open
{
BOOL success;
NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"Mohan.sqlite"];
// _DBPath=[[NSBundle mainBundle] pathForResource:@"Mohan" ofType:@"sqlite"];
//_DBPath=[NSString stringWithFormat:@"/var/mobile/Applications/C6088ECC-17F3-4A86-814C-7D816B7A49BB/Documents/Mohan.sqlite"];


NSLog(@"Database path: %@",databasePath);

NSFileManager *fileManager=[NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];

if (success)
{
    _DBPath=databasePath;
}
else
{
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Mohan.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
    _DBPath=defaultDBPath;
    if (!success)
    {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }

}


-(void)insertValue:(NSString *)query
{
sqlite3_stmt *statement;

[self open];
if (sqlite3_open([_DBPath UTF8String], &_DB) == SQLITE_OK)
{

    if (sqlite3_prepare_v2(_DB, [query UTF8String], -1, &statement, NULL)==SQLITE_OK)
    {

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Data Inserted");
            NSLog(@"%@",query);

        }
        else
        {
            NSLog(@"Failed to Insert");
        }

    }

}
NSLog(@"ERROR MSG: %@", [NSString stringWithUTF8String:(char*)sqlite3_errmsg(_DB)]);
NSLog(@"Last inserted row id: %lld",sqlite3_last_insert_rowid(_DB));
sqlite3_finalize(statement);

sqlite3_close(_DB);
NSLog(@"db closed");

}

插入值发生错误,显示无法打开数据库

0 个答案:

没有答案