需要有关插入数据库的帮助
我是新手...我需要插入语句的帮助..我的代码如下..代码运行但它不会在数据库内添加任何内容。请任何人告诉我为什么会这样..
- (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];
}
}
谢谢家伙,我明白了...编辑了上面的代码,现在它运行并在数据库中提交数据..
i m a newbie... I need help with inser statement .. my code is as follows.. the code runs but it does not add anything inside the database. plz can anyone suggest me why is it so..
- (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 i got it... edited the above code now it runs and submit data in database..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将值绑定到准备好的语句,但看起来您从未调用过 commit。
You bind the values to the prepared statement, but it does not look like you ever call commit.