gpt4 book ai didi

iphone - 向表中插入数据时出现问题

转载 作者:行者123 更新时间:2023-12-03 20:16:21 24 4
gpt4 key购买 nike

sqlite3 *数据库; sqlite3_stmt *语句; NSString *dPath = [[[NSBundle mainBundle] resourcesPath] stringByAppendingPathComponent:@"UserData.sqlite"]; const char *dbpath = [dPath UTF8String]; //NSLog(@"%@",dbpath); //UserArray = [[NSMutableArray alloc]init];

if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (UserName,FullName,Email,PhoneNo) VALUES (\"%@\", \"%@\", \"%@\" ,\"%@\")",txtUserName.text,txtFullName.text ,txtEmail.text , txtPhoneNo.text];

const char *insert_stmt = [insertSQL UTF8String];

sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);

if(sqlite3_step(statement)==SQLITE_DONE)
{
txtUserName.text=@"";
txtFullName.text=@"";
txtEmail.text=@"";
txtPhoneNo.text=@"";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;

}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;
}


// Release the compiled statement from memory
sqlite3_finalize(statement);

sqlite3_close(database);
}

->> 我正在尝试将数据插入表中。代码执行正常,但记录未添加到表中。所以请帮助我或这个问题..感谢您的回复。

最佳答案

尝试执行提交,因此将数据库设置为自动提交

// COMMIT
const char *sql2 = "COMMIT TRANSACTION";
sqlite3_stmt *commit_statement;
if (sqlite3_prepare_v2(database, sql2, -1, &commit_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database));
}
success = sqlite3_step(commit_statement);
if (success != SQLITE_DONE) {
NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database));
}
}

关于iphone - 向表中插入数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4890315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com