3

我正在使用 SQLite 数据库来存储一些信息,例如图像名称、日期我在尝试插入数据时遇到异常并且它使应用程序崩溃。

我已经通过放置断点和异常断点来检查它,它崩溃了,因为它无法找到表但表已经存在。

看到这个: 在此处输入图像描述

这是我得到的崩溃报告:

*** Assertion failure in -[PhotosPage savePhotosToDB], /Users/apple/Desktop/Mayur_iPhone/My Home Projects/Daily Photos Application/29thMar2013_DailyPhotos/iPhone/PhotosPage.m:168
2013-03-29 14:21:53.398 DailyPhotos[881:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while creating add statement. 'no such table: PhotosTable''
*** First throw call stack:
(0x1b11012 0x1606e7e 0x1b10e78 0x5e5f35 0x9014 0x870d 0x161a705 0x92f920 0x92f8b8 0x9f0671 0x9f0bcf 0x9efd38 0x95f33f 0x95f552 0x93d3aa 0x92ecf8 0x26d7df9 0x26d7ad0 0x1a86bf5 0x1a86962 0x1ab7bb6 0x1ab6f44 0x1ab6e1b 0x26d67e3 0x26d6668 0x92c65c 0x7cad 0x1d45)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

这是我用于插入数据的代码:

-(void)checkAndCreateDatabase
{
    BOOL success;
    databaseName = @"DailyPhotosDB.rdb";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success) return;
    NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"DailyPhotosDB" ofType:@"sqlite"];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    [fileManager release];
}

-(void)savePhotosToDB
{
    [self checkAndCreateDatabase];
    sqlite3 *database;
    static sqlite3_stmt * addStmt ;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    databaseName = @"DailyPhotosDB.rdb";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success = [fileManager fileExistsAtPath:databasePath];
    NSDateFormatter* df = [NSDateFormatter new];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSString *strCurDate = [df stringFromDate:[NSDate date]];

    if(!success)
    {
        NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"DailyPhotosDB" ofType:@"sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "insert into PhotosTable(PhotoName,CreationDate) Values(?,?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        sqlite3_bind_text(addStmt, 1, [timeString UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 2, [strCurDate UTF8String], -1, SQLITE_TRANSIENT);
        if(SQLITE_DONE != sqlite3_step(addStmt))
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
        else
            sqlite3_reset(addStmt);
    }
    sqlite3_close(database);
}

提前致谢...

4

3 回答 3

5

试试这个代码

NSString *strquery=[NSString stringWithFormat:@"insert into foodfinder(nameOfRestaurants,contacts,address,lats,longs) values(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",nameOfRestaurants,contacts,address,lats,longs];
return [AppDelegate_iPhone executeQuery:strquery];

编辑->下面的代码来自评论

NSString *strsrspath=[[NSBundle mainBundle]pathForResource:@"food" ofType:@"rdb"]; 
NSString *strdestpath=[NSString stringWithFormat:@"%@/Documents/food.rdb",NSHomeDirectory()]; 
NSFileManager *manager=[NSFileManager defaultManager]; 
if ([manager fileExistsAtPath:strdestpath]) {
 NSLog(@"File Already Exist");
 } else { 
NSError *err; [manager copyItemAtPath:strsrspath toPath:strdestpath error:&err]; 
NSLog(@"file successfully copied"); 
return YES; 
} 
return NO;
于 2013-03-29T10:25:50.073 回答
3

实际上你需要删除你之前创建的数据库,然后将 sqlite 文件添加到你的包中,这将解决你的问题。

于 2014-08-08T11:36:27.443 回答
1

我在实现数据库时遇到了同样的问题并通过以下方式解决了它。

-> 转到目标 -> 构建阶段 -> 复制捆绑资源单击 + 并从资源中选择 Sqlite 数据库 rdb 文件。

希望这可以帮助。

于 2013-04-16T07:32:17.440 回答