'创建添加语句时出错'没有这样的表

时间:2013-03-29 09:04:14

标签: iphone objective-c ipad iphone-5

我正在使用SQLite数据库来存储一些信息,比如图像名称,日期我正在尝试插入数据&它崩溃了应用程序。

我已经通过提供突破点和放弃点来解决这个问题。异常断点崩溃,因为它无法找到表,但表已经存在。

看到这个: enter image description here

这是我得到的崩溃报告:

*** 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);
}

提前致谢...

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:3)

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

答案 2 :(得分:1)

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

- >转到目标 - >构建阶段 - >复制捆绑资源点击它上面的+&从资源中选择Sqlite Database rdb文件。

希望这有助于。