我一直在尝试将高分保存到数据库中并且过去一周一直失败,我不知道为什么它不起作用。我不断收到“准备语句问题”并拒绝将信息插入数据库。我已经检查了数据库管理器以确保 sql 语句没有拼写错误,并且当在管理器上运行查询时,它工作正常 - 只是 iphone 给了我这个问题。如果有人可以快速查看并发现有问题并告诉我,我将不胜感激!
- (NSMutableArray *) saveLocal {
NSLog(@"save local database");
@try {
[self checkDB];
sqlite3_stmt *sqlStatement2;
NSString *sqlS = [NSString stringWithFormat:@"INSERT INTO localHighscore (difficulty, score, uname, puzzles, multiplier, oneshots, hints) VALUES (%i,%i,\"%@\",%i,%i,%i,%i)",[[MySingleton sharedMySingleton] goDifficulty],[[MySingleton sharedMySingleton] goScore],_player, [[MySingleton sharedMySingleton] goPuzzles], [[MySingleton sharedMySingleton] goMultiplier], [[MySingleton sharedMySingleton] goOneshots], [[MySingleton sharedMySingleton] goHints]];
NSLog(@"%@",sqlS);
const char *sql = [sqlS UTF8String];
if(sqlite3_prepare_v2(localHighscore, sql, -1, &sqlStatement2, NULL) == SQLITE_OK)
{
sqlite3_step(sqlStatement2);
sqlite3_reset(sqlStatement2);
sqlite3_finalize(sqlStatement2);
NSLog(@"save complete");
} else {
NSLog(@"Problem with prepare statement");
}
sqlite3_close(localHighscore);
}@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}@finally{
NSLog(@"DB Loaded!");
}
}
这是 checkDB 方法,它检查数据库是否存在,如果不存在则创建一个
- (void)checkDB {
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"localHighscore.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
NSLog(@"file was not found");
if (sqlite3_open(dbpath, &localHighscore) == SQLITE_OK)
{
NSLog(@"db open");
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS localHighscore(pk INTEGER PRIMARY KEY AUTOINCREMENT, difficulty TINYINT, score MEDIUMINT, uname VARCHAR(255), puzzles TINYINT, multiplier TINYINT, oneshots TINYINT, hints TINYINT)";
if (sqlite3_exec(localHighscore, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
sqlite3_close(localHighscore);
} else {
NSLog(@"Failed to open/create database");
}
}
[filemgr release];
}
在此先感谢您的帮助!