gpt4 book ai didi

iphone - “创建添加语句时出错”没有这样的表

转载 作者:搜寻专家 更新时间:2023-10-30 19:54:58 25 4
gpt4 key购买 nike

我正在使用 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);
}

提前致谢...

最佳答案

试试这个代码

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;

关于iphone - “创建添加语句时出错”没有这样的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15700251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com