gpt4 book ai didi

ios - sqlite3_step(statement) == SQLITE_DONE 总是假的

转载 作者:行者123 更新时间:2023-12-01 19:10:27 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中注册一个用户。我有一个带有 3 textfields (username, password and confirmpassword) 的 View Controller 和 submit按钮。

按下提交按钮时会调用以下方法:

-(IBAction)addUser
{
NSString *tempUser,*tempPass, *tempConfPass;
tempUser = [[NSString alloc]init];
tempPass = [[NSString alloc]init];
tempConfPass = [[NSString alloc]init];
tempUser = [NSString stringWithFormat:@"%@",_mUserName.text];
tempPass = [NSString stringWithFormat:@"%@",_mPassword.text];
tempConfPass = [NSString stringWithFormat:@"%@",_mConfPassword.text];
signupUser = [[UseDb alloc]init];

flagUser = [signupUser addNewUser:_mUserName.text:_mPassword.text:_mConfPassword.text];
if(flagUser)
{
myAlertViewUser = [[UIAlertView alloc] initWithTitle:@"Error" message:@"User Added"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[myAlertViewUser show];
}
else {
_mStatus.text = @"failed to add user";
myAlertViewUser = [[UIAlertView alloc] initWithTitle:@"Error" message:@"passwords don't match"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[myAlertViewUser show];
}
}

此方法由 addUser 调用方法:
-(BOOL)addNewUser:(NSString *)newUser :(NSString *)newPassword :(NSString *)confirmPass
{
NSLog(@"%@....%@...%@",newUser, newPassword, confirmPass);
sqlite3_stmt *statement;
const char *dbpath = [_mDatabasePathDb UTF8String];

if (sqlite3_open(dbpath, &_mDb) == SQLITE_OK && [newPassword isEqualToString:confirmPass] && ![newUser isEqualToString:@""] && ![newPassword isEqualToString:@""])
{
self.userName = [NSString stringWithFormat:@"%@",newUser];
self.password = [NSString stringWithFormat:@"%@",newPassword];

NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO USERDETAIL VALUES (\"%@\",\"%@\",\"%@\", \"%@\",\"%@\", \"%@\",\"%@\", \"%@\",\"%@\", \"%@\",\"%@\")",self.userName,self.password,@"",@"",@"",@"",@"",@"",@"",@"",@"" ];

NSLog(@"%@",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, NULL);

if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
/* mUserName.text = @"";
mPassword.text = @"";
mConfPassword.text = @""; */

}
else {

NSLog(@"failed to add user");
}

sqlite3_finalize(statement);
sqlite3_close(_mDb);
}
}

addNewUser方法, if (sqlite3_step(statement) == SQLITE_DONE)总是出来是 false , 声明之前有一些值(value)
sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, NULL); 

但转向 nil上述语句执行后。我不明白为什么会这样。请帮忙。

最佳答案

这可能是您正在进行的检查的问题,请尝试 SQLITE_OK。它在 docs 中说如在旧接口(interface)(旧接口(interface))中,返回值将是 SQLITE_BUSY、SQLITE_DONE、SQLITE_ROW、SQLITE_ERROR 或 SQLITE_MISUSE .与 “v2”接口(interface) ,也可能返回任何其他结果代码或扩展结果代码。

if (sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, nil) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else
{
NSLog(@"failed to add user");
}
sqlite3_finalize(statement);
}

您还可以找到类似的问题及其答案 here

在prepare 语句后插入一个花括号,并在finalize 语句后关闭它。当你得到 SQLITE_MISUSE 时,可能是这个例程被不恰本地调用了。也许它是在已经完成的准备好的语句上调用的,或者是在之前返回 SQLITE_ERROR 或 SQLITE_DONE 的语句上调用的。或者可能是同一数据库连接同时被两个或多个线程使用。

希望这可以帮助 :)

关于ios - sqlite3_step(statement) == SQLITE_DONE 总是假的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891190/

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