我正在使用 this使用 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')"];
关于ios - sqlite3 不接受文本中的空格(Objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42264812/