0
嗯,我正在更新我创建的应用程序,但我遇到了一些运行时问题。人们说他们希望能够保存他们输入的数据,所以我正在实施一个SQLite数据库。如果数据库或数据库表不存在,我得到应用程序来检查数据库是否存在并创建所需的所有内容。但是,我有麻烦从SQLite中检索数据。当我按下应该加载数据的按钮时,应用程序崩溃。我试图使用NSLog和UIAlertView来找出发生了什么,但我无法检索结果。但是,在SQLite中执行测试运行时,我发布的查询是正确的,因此必须有其他内容。尝试从SQLite加载时应用程序崩溃
下面是我使用检索数据的代码:
- (IBAction)loadData
{
NSDateFormatter* bf = [[NSDateFormatter alloc] init];
[bf setDateFormat:@"MM/dd/yyyy"];
NSDate* sdate;
NSString* birthDate;
NSString* balance;
NSString* dyear;
sqlite3_stmt *stmt;
// get document directory
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
// build path to database
dbpath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"mrd.db"]];
const char *dpath = [dbpath UTF8String];
if (sqlite3_open(dpath, &mrdDB) == SQLITE_OK)
{
NSString* query = [NSString stringWithFormat:@"SELECT date(birth), bal, year FROM rmd LIMIT 1"];
const char* query_statement = [query UTF8String];
if (sqlite3_prepare_v2(mrdDB, query_statement, -1, &stmt, NULL) == SQLITE_OK)
{
if (sqlite3_step(stmt) == SQLITE_ROW)
{
birthDate = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
balance = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];
dyear = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 3)];
sdate = [bf dateFromString:birthDate];
self.birth.text = [bf stringFromDate:sdate];
[birthDate release];
[bf release];
self.bal.text = balance;
[balance release];
self.year.text = dyear;
[dyear release];
self.status.text = @"data loaded sucessfully";
} else {
self.status.text = @"Cannot find/retrieve saved data";
}
sqlite3_finalize(stmt);
}
sqlite3_close(mrdDB);
}
}
该应用程序能够将数据保存到数据库中,作为应用显示“保存成功”的消息,这是我增加了对用户的缘故以及如果事情做得对,所以我不太确定发生了什么。
错误说的是什么? – PaulG 2012-04-17 18:55:09
报告的错误指向“int retVal = UIApplicationMain(argc,argv,nil,nil);”并且错误是“Program received signal:'SIGABRT'”(单引号在这里用双引号括起来)。 – user1013391 2012-04-17 18:57:16
看看你的堆栈,找到有问题的代码行。或者同样简单,在这个例程中设置一个调试断点并逐步完成,看看你有什么问题。我想知道如何将日期检索为字符串,将其转换为日期并将其转换回字符串。例如,如果sdate是零(因为它符合你的dateformatter格式字符串),我不会感到惊讶。我不知道。但是,通过这个例程一步,你的问题就会跳出来。 – Rob 2012-04-18 02:47:31