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

时间:2017-02-16 04:13:42

标签: ios objective-c sqlite

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

我使用NSDate获取当前日期,并将其放在UILabel而不是UITextField中。

现在,当我按保存时,我收到“语法错误”。我已经尝试使用当前年份并且它有效,因为它是一个整数,即使表中的列设置为“text”。像“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.");
    }
}

3 个答案:

答案 0 :(得分:1)

您必须在''中添加文字值。 EX。

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

答案 1 :(得分:0)

尝试此查询

  

插入peopleInfo(peopleInfoID,firstname,date)值(1,   '杜鹃',' 2016年2月16日')

它为我工作!

答案 2 :(得分:0)

您需要将'围绕日期格式说明符%@添加到'%@',如下所示

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];
}