sqlite sqlite3_column_text返回变量不是NSString错误

时间:2013-01-28 11:50:46

标签: iphone sql string sqlite char

这是我的sqlite声明:

NSString *queryString = @"SELECT email.pk, email.sender_name, email.sender_address, search_email.subject, email.datetime, "
"LENGTH(email.attachments), SUBSTR(search_email.body,0,150), email.folder_num, t.threadid, t.numThreads FROM "
"email, search_email ,(select  threadid, min( docid) as docid, count(*) as numThreads from search_email group by threadid) as t       "
"WHERE t.docid = email.pk AND (email.folder_num = ? OR email.folder_num_1 = ? OR email.folder_num_2 = ? OR email.folder_num_3 = ?) AND email.pk = search_email.docid "
"ORDER BY email.datetime DESC;";

我使用sqlite3_column_text获取的所有值和sqlite3_column_int工作正常..但由于某种原因它在threadid上失败:

NSString* temp = @"";
const char *sqlVal = (const char *)sqlite3_column_text(folderSearchStmt, 1);

...
temp = @"";
sqlVal = (const char *)sqlite3_column_text(folderSearchStmt, 8);
if(sqlVal != nil)
    temp = [NSString stringWithUTF8String:sqlVal];
[res setObject:temp forKey:@"threadID"];

如果我在sqlVal上设置断点,则返回sqlVal = (const char*) 0x266c0a30 "1-87" 但然后temp返回(__NSCFString*)0x27f7bff0 @<variable is not NSString>

任何想法?

1 个答案:

答案 0 :(得分:0)

必须拒绝这样的无效条目:

      const char *_abc = (char *) sqlite3_column_text(statement, 1);  

   NSString *_abcTemp;

      if (_abc)
          _abcTemp = _abc == NULL ? nil : [[NSString alloc] initWithUTF8String:_abc];
     else 
        _abcTemp = [[NSString alloc] initWithString:@"Not a valid string"];