【发布时间】:2014-12-16 13:41:50
【问题描述】:
我正在运行本教程sqlite database iOS app tutorial中的代码。查询功能是:
- (IBAction)saveInfo:(id)sender
{
// Prepare the query string.
// If the recordIDToEdit property has value other than -1, then create an update query. Otherwise create an insert query.
NSString *query;
if (self.recordIDToEdit == -1)
{
query = [NSString stringWithFormat:@"insert into peopleInfo values(null, '%@', '%@', '%@')", self.txtFirstname.text, self.txtLastname.text, self.txtAge.text ];
}
else
{
query = [NSString stringWithFormat:@"update peopleInfo set firstname='%@', lastname='%@', age='%@' where peopleInfoID=%d", self.txtFirstname.text, self.txtLastname.text, self.txtAge.text, self.recordIDToEdit];
}
NSLog(@"The query is: %@",query);
// Execute the query.
[self.dbManager executeQuery:query];
// If the query was succesfully executed then pop the view controller.
if (self.dbManager.affectedRows != 0) {
NSLog(@"Query was executed successfully. Affacted rows = %d", self.dbManager.affectedRows);
// Inform the delegate that editing was finished.
[self.delegate editingInfoWasFinished];
// Pop the view controller.
[self.navigationController popViewControllerAnimated:YES];
}
else
{
NSLog(@"Could not execute the query");
}
}
程序在插入记录时出现 sql 错误
The query is: insert into peopleInfo values(null, 'Project 1', 'Test 1', '2014-12-15 05:00:20 +0000')
DB Execute Query Error: SQL logic error or missing database
Could not execute the query.
如何消除这个错误?
【问题讨论】:
-
插入查询中的 null 是什么意思。该列是标识还是整数?检查列是否不为空。它是否适用于更新查询?
-
您是否已将数据库文件夹(随您引用的代码一起提供)导入到您的项目中?你可能应该错过它。因为错误显示缺少数据库。
-
您是否在应用程序中指定了数据库路径
-
如何指定?我已经使用这个命令完成了:self.dbManager = [[DBManager alloc] initWithDatabaseFileName:@"db.sql"];in viewdid load
-
@frincit:是的,那是正确的,你必须将文件复制到文档目录
标签: ios objective-c sqlite