【发布时间】:2012-07-21 05:57:55
【问题描述】:
我是一名初学 iPhone 开发人员,试图从 Xcode 4.3 中的 sqlite 数据库中获取信息。我的数据库(名为 DB_Info.sqlite)与 .h 和 .m 文件位于同一目录中,并且我还将数据库拖到 Xcode 左侧栏中的文件夹部分。
能否请您快速查看一下我的代码并告诉我我的错误在哪里?我使用 NSLogs 在最后一个 if 语句中确定问题发生的位置,并且它是用 cmets 编写的。提前非常感谢您!
#import <sqlite3.h>
@implementation Player
{
sqlite3 *DB_Info;
NSString *databasePath;
NSString *docsDir;
NSArray *dirPaths;
}
-(Player*)createPlayer:(NSString*)playerName
{
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"DB_Info.sqlite"]];
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &DB_Info) == SQLITE_OK) { //works fine
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM playerlist WHERE fullName=\"%@\"", playerName];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(DB_Info, query_stmt, -1, &statement, NULL) == SQLITE_OK) { //PROBLEM: This is where the problem is, and the if statement never goes through
//....rest of code here
} else {
NSLog(@"Error");
}
}
【问题讨论】:
-
NSLog(@"%@", querySQL);并仔细检查 querySQL 是正确的查询
-
您正在打开文档文件夹中的数据库。你那里抄的?因为如果它不存在,sqlite3_open 将为您创建一个空白数据库。您大概 (a) 想要将 db 从捆绑包复制到文档(假设您将其包含在捆绑包中); (b) 使用 sqlite3_open_v2 而不是 sqlite_open。
标签: objective-c ios database xcode sqlite