【问题标题】:DB Execute Query Error: SQL logic error or missing database xcode数据库执行查询错误:SQL 逻辑错误或缺少数据库 xcode
【发布时间】: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


【解决方案1】:

查看突出显示的文件夹。将其复制到您的项目中。您的代码似乎很好。并确保 db.sql 在那里。

【讨论】:

  • 好的尝试重置模拟器。重置内容和设置。并重新运行应用程序。您是否正在运行示例文件本身?还是您自己也对其进行了修改?因为我已经下载了代码,它在这里运行完美。
  • 身份证。示例文件在这里完美运行。
  • 没有更多线索。我们被困在这里哈哈。您是自己运行示例代码还是在某处调整了代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多