ios - 尝试建立 SQLite 连接/语句时应用程序崩溃

标签 ios xcode sqlite

我在使用 SQLite 时遇到问题,当我尝试连接到 SQLite 数据库并获取其部分内容时,我的应用程序在运行时崩溃。这是我的代码:

-(IBAction)setInput:(id)sender
{
    NSString *strStoreNumber;
    NSString *strRegNumber;

    strStoreNumber = StoreNumber.text;
    strRegNumber = RegNumber.text;
    lblStoreNumber.text = strStoreNumber;
    lblRegNumber.text = strRegNumber;

    NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    {
        NSLog(@"Opened sqlite database at %@", databasePath);
        //...stuff
    } 
    else 
    {
        NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
        sqlite3_close (database);
    }

    NSString *querystring;

    querystring = [NSString stringWithFormat:@"SELECT strStore FROM tblStore WHERE strStore = %@;", strStoreNumber];  

    const char *sql = [querystring UTF8String];

    NSString *szStore = nil;
    NSString *szReg = nil;

    sqlite3_stmt *statement = nil;
    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL)!=SQLITE_OK) //queryString = Statement
    {
        NSLog(@"sql problem occured with: %s", sql);
        NSLog(@"%s", sqlite3_errmsg(database));
    }
    else
    {
        // you could handle multiple rows here
        while (sqlite3_step(statement) == SQLITE_ROW) // queryString = statement
        {            
            szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
            szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
        } // while        

    }

    sqlite3_finalize(statement);    
    // go on with putting data where you want    
} 

我在控制台中遇到的错误:

2012-05-07 09:39:33.619 CCoDBTry[962:f803] Opened sqlite database at /Users/*******/Library/Application Support/iPhone Simulator/5.1/Applications/5DB7A218-A0F6-485F-B366-91FD2F9BC062/CCoDBTry.app/tblStore.sqlite
2012-05-07 09:39:33.623 CCoDBTry[962:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(0x1593022 0x1724cd6 0x153ba48 0x153b9b9 0x9d4973 0x27c9 0x1594e99 0xd714e 0xd70e6 0x17dade 0x17dfa7 0x17d266 0xfc3c0 0xfc5e6 0xe2dc4 0xd6634 0x147def5 0x1567195 0x14cbff2 0x14ca8da 0x14c9d84 0x14c9c9b 0x147c7d8 0x147c88a 0xd4626 0x1f82 0x1ef5)
terminate called throwing an exception(lldb)

非常感谢任何帮助!

编辑:注意我在数据库中寻找的值是 VARCHAR 类型。我不确定知道这是否有所作为。

最佳答案

+[NSString stringWithUTF8String:]: NULL cString

它提示您向 stringWithUTF8String: 中输入了空值。您的代码对该方法有两次调用,每次都从查询结果中输入一个值。查看您的查询,您只选择了一个列 strStore。我假设分配给 szReg 的行失败了,因为您正在尝试检索您未在查询中选择的值。

关于ios - 尝试建立 SQLite 连接/语句时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10483138/

相关文章:

ios - NSMutableString iOS 中 nil 和 @""的区别

ios - ARC + Dealloc 不调用

ios - 如何设置屏幕高度一半的 View ?

iphone - 允许触摸通过透明图像

java - 如何在 Android 中获取类(非 Activity )的上下文?

ios - 在 Xcode 7 中更改了 +load 方法顺序

ios - Firebase 数据库结构和查询格式选项

ios - 在 Xcode 4.5 "Show Live Issues"中不再起作用

ios - iOS 中的数据模型持久性注意事项

ios - 将子实体追加到核心数据 iOS Swift 中的实体