sqlite3 不接受文本中的间距 (Objective-C)



我正在使用本教程使用SQLITE3数据库为iOS创建应用程序。但是,而不是拥有名字,姓氏和年龄;我有名字和当前日期。

我使用nsdate获取当前日期,然后将其放在uilabel而不是uitextfield。

现在,当我按"保存"时,我会收到"语法错误"。我已经尝试使用本年度,并且可以使用它,因为它是一个整数,即使表中的列设置为"文本"。诸如" 2017年2月16日"之类的间距的约会似乎也没有起作用,因为它的间距有间距。

我希望能够以这种格式保存它: 2017年2月16日

谢谢!

- (void)viewDidLoad {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
//Get the date today.
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
[super viewDidLoad];
// Do any additional setup after loading the view.
self.txtFirstname.delegate = self;
[_txtAge setText:dateToday];
- (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.txtDate.text];
    }
    else{
        query = [NSString stringWithFormat:@"update peopleInfo set firstname='%@', date=%@ where peopleInfoID=%d", self.txtFirstname.text, self.txtDate.text, self.recordIDToEdit];
    }

    // Execute the query.
    [self.dbManager executeQuery:query];
    // If the query was successfully executed then pop the view controller.
     if (self.dbManager.affectedRows != 0) {
        NSLog(@"Query was executed successfully. Affected rows = %d", self.dbManager.affectedRows);
        // Inform the delegate that the editing was finished.
        [self.delegate editingInfoWasFinished];
        // Pop the view controller.
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        NSLog(@"Could not execute the query.");
    }
}

您必须在''中添加文本值。前任。

query = [NSString stringWithFormat:@"insert into peopleInfo (peopleInfoID,firstname,date) values (1, 'user name', '16 FEB 2016')"];

您需要将 '围绕日期格式 %@放置在 '%@'上,如下

if (self.recordIDToEdit == -1) {
    query = [NSString stringWithFormat:@"insert into peopleInfo values(null, '%@', '%@')", self.txtFirstname.text, self.txtDate.text];
}
else{
    query = [NSString stringWithFormat:@"update peopleInfo set firstname='%@', date='%@' where peopleInfoID=%d", self.txtFirstname.text, self.txtDate.text, self.recordIDToEdit];
}

尝试此查询

插入PeopleInfo(PeopleInfoid,firstName,date)值(1, 'cuckoo','2016年2月16日)

它对我有用!

最新更新