我是新手......我需要帮助inser语句..我的代码如下..代码运行但它不会在数据库中添加任何内容。 PLZ任何人都可以建议我为什么会这样..
- (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];
}
}
THanks guyz我得到了...编辑上面的代码现在它运行并在数据库中提交数据..
答案 0 :(得分:0)
将值绑定到预准备语句,但它看起来不像是调用commit。