gpt4 book ai didi

iOS - 从 UIAlertView 添加数据到 sqlite

转载 作者:行者123 更新时间:2023-11-28 22:16:00 25 4
gpt4 key购买 nike

我想询问用户是否想将他在游戏中的分数添加到高分中,并且在输入中我想询问他的名字。他完成后我有这样的代码:

ListOfScores *listOfScores =[[ListOfScores alloc] init];
if ([listOfScores GetScoresCount] < 10) {
[self ShowMessageBoxForHighScore];
}
else if([listOfScores GetLastScore] < self.ActualScore)
{
[self ShowMessageBoxForHighScore];
}

- (void)ShowMessageBoxForHighScore
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"save score" message:@"input username:" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];

alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = 12;

[alert addButtonWithTitle:@"save"];
[alert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 12) {
if (buttonIndex == 1) {
UITextField *textfield = [alertView textFieldAtIndex:0];
ListOfScores *listOfScores =[[ListOfScores alloc] init];
Score* score = [[Score alloc] initWithName:textfield.text Points:self.ActualScore];
[listOfScores AddScore:score];
}
}
}

这些是我检查和创建数据库的方法:

-(BOOL)CreateTableForScores {
BOOL ret;
int rc;
// SQL to create new table

NSString *sql_str = @"CREATE TABLE Scores (pk INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , Points INTEGER DEFAULT 0, Name VARCHAR(100))";

const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(@"query %s",sqlStatement);

sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, sqlStatement, -1, &stmt, NULL);

ret = (rc == SQLITE_OK);
if (ret)
{ // statement built, execute
rc = sqlite3_step(stmt);
ret = (rc == SQLITE_DONE);
}

sqlite3_finalize(stmt); // free statement
NSLog(@"creating table");
return ret;
}

-(BOOL)TableForScoresExists {
sqlite3_stmt *statementChk;
sqlite3_prepare_v2(db, "SELECT name FROM sqlite_master WHERE type='table' AND name='Scores';", -1, &statementChk, nil);

bool boo = FALSE;

if (sqlite3_step(statementChk) == SQLITE_ROW) {
boo = TRUE;
}
sqlite3_finalize(statementChk);

return boo;
}

和我的 AddScore 方法:

- (void) AddScore:(Score *)newScore
{
@try {
BOOL tableExists = self.TableForScoresExists;
if(!tableExists)
{
tableExists = self.CreateTableForScores;
}

if(tableExists)
{
NSString *filename = @"database.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:filename];
NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *documentsPath = [documentsFolder stringByAppendingPathComponent:filename];
//NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Milionar.sqlite"];
const char *sql = "INSERT INTO Scores(Points,Name) VALUES(?,?)";


if (![fileManager fileExistsAtPath:documentsPath])
{
BOOL success;
NSError *error = nil;
success = [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error];
NSAssert(success, @"Unable to copy database: %@", error);
}

if(sqlite3_open([documentsPath UTF8String], &db) == SQLITE_OK)
{
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_int(sqlStatement, 1, newScore.Points); //
sqlite3_bind_text(sqlStatement, 2, [newScore.Name UTF8String], -1, SQLITE_TRANSIENT); //

if (sqlite3_step(sqlStatement) != SQLITE_DONE)
{
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(db));
}
sqlite3_finalize(sqlStatement);

}
else
{
NSLog(@"Problem with prepare statement");
}
}
else
{
NSLog(@"An error has occured while opening database.");
}

sqlite3_close(db);

}

}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}

}

问题是在 AddScore 方法中,当我尝试检查数据库是否存在时,我得到了 false。但是当我不使用 UIAlertView 并且我用一些文本替换 AddScore 的调用警报时,我得到了一切正常并且我保存了 Score 对象。哪里有问题?为什么它在委托(delegate)方法中不起作用?谢谢

最佳答案

CreateTableForScores 和 TableForScoresExists 都需要一个有效的数据库指针。

因此您需要先打开数据库,然后再调用这两个属性中的任何一个。

在您当前的代码片段中,可能会使用未初始化的数据库指针调用这两个函数。

此外,如果您使用“CREATE TABLE IF NOT EXISTS”(https://www.sqlite.org/lang_createtable.html),则不一定需要调用方法 TableForScoresExists这为您节省了一些代码。

关于iOS - 从 UIAlertView 添加数据到 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662114/

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