我是新手...我需要有关插入语句的帮助..我的代码如下..代码运行但它不会在数据库中添加任何内容。请问谁能告诉我为什么会这样..
- (IBAction)add:(id)Sender{
CGPoint loc;
loc.x = [_x.text floatValue];
loc.y = [_y.text floatValue];
if(loc.x != 0 || loc.y != 0 )
{
[_x endEditing:YES];
[_y endEditing:YES];
[_name endEditing:YES];
NSString *date = [[NSDate alloc]init];
NSNumber *x = [NSNumber numberWithFloat:loc.x];
NSNumber *y = [NSNumber numberWithFloat:loc.y];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into table (name, xLoc,yLoc,date) VALUES ( ?, ?, ?, ?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text( compiledStatement, 1, [_name.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double( compiledStatement, 2, [x doubleValue]);
sqlite3_bind_double(compiledStatement, 3, [y doubleValue]);
sqlite3_bind_text(compiledStatement, 4, [date UTF8String] ,-1, SQLITE_TRANSIENT);
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
NSLog( @"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( @"Insert into row id = %lld", sqlite3_last_insert_rowid(database));
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
[[self navigationController]popViewControllerAnimated:YES];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Wrong Co-ordinates described." delegate:nil cancelButtonTitle:@"Go Back" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
谢谢 guyz 我知道了...编辑上面的代码现在它运行并在数据库中提交数据..
最佳答案
您将值绑定(bind)到准备好的语句,但看起来您从未调用过提交。
关于database - 需要帮助插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950101/