2 daffodilgirl DaffodilGirl 于 2013.01.29 14:36 提问

sqlite sqlite3_column_text返回变量

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"];

如果我在aslVal加一个断点,返回sqlVal = (const char*) 0x266c0a30 "1-87",但是随后temp返回(__NSCFString*)0x27f7bff0 @<variable is not NSString>

1个回答

redCoral_
redCoral_   2013.01.29 15:53
已采纳

需要拒绝无效的条目,如下:

      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"];  
Csdn user default icon
上传中...
上传图片
插入图片