【问题标题】:How to get original special characters from SQLite db using iPhone SDK?如何使用 iPhone SDK 从 SQLite db 获取原始特殊字符?
【发布时间】:2012-11-30 13:09:11
【问题描述】:

我正在将 HTML 内容(具有项目符号等特殊字符)插入 SQLite 数据库。

当我尝试获取视图上的内容时,它没有正确显示特殊字符。它向我显示垃圾文本。

如何确保我在数据库中插入的任何文本都能正确显示在视图中。

谢谢!

我的插入代码:

//这个查询方法实现在不同的文件中

- (NSArray *)executeQuery:(NSString *)sql arguments:(NSArray *)args {
    sqlite3_stmt *sqlStmt;

    if (![self prepareSql:sql inStatament:(&sqlStmt)])
        return nil;

    int i = 0;
    int queryParamCount = sqlite3_bind_parameter_count(sqlStmt);
    while (i++ < queryParamCount)
        [self bindObject:[args objectAtIndex:(i - 1)] toColumn:i inStatament:sqlStmt];

    NSMutableArray *arrayList = [[NSMutableArray alloc] init];   // By Devang
    int columnCount = sqlite3_column_count(sqlStmt);
    while ([self hasData:sqlStmt]) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        for (i = 0; i < columnCount; ++i) {
            id columnName = [self columnName:sqlStmt columnIndex:i];
            id columnData = [self columnData:sqlStmt columnIndex:i];
            [dictionary setObject:columnData forKey:columnName];
        }
        [arrayList addObject:dictionary];

        //[arrayList addObject:[dictionary autorelease]];
    }

    sqlite3_finalize(sqlStmt);

    return arrayList;
}

// 现在通过 make 对象为这个文件调用这个方法

NSString *inserQuery =[NSString stringWithFormat:@"insert into feedtest (title,summary,image,id) values ('%@','%@','%@',%d)",cell.textLabel.text,source,returnURL,indexPath.row];
        NSLog(@"query - %@",inserQuery);

    [database executeQuery:inserQuery];

// 检索数据

NSString *sd=[NSString stringWithFormat:@"Select title,summary from feedtest"];

    NSMutableArray *p=[[NSMutableArray alloc]init];
    p=[[database executeQuery:sd ] mutableCopy];
    [database close];

    NSString *titleHTML = [[p objectAtIndex:i]valueForKey:@"title"];
    NSString *postHTML =[[p objectAtIndex:i]valueForKey:@"summary"];
    NSLog(@"%@",titleHTML);
    NSLog(@"%@",postHTML);

【问题讨论】:

  • 请发布您的 INSERTION 和 SELECTION 代码
  • 当你 NSLog(@"query - %@",inserQuery);您在控制台上看到 HTML 字符吗?我猜你在解析从网络接收到的数据时删除了 HTML
  • 是的,垃圾文本显示在控制台中

标签: iphone objective-c ios cocoa-touch sqlite


【解决方案1】:

您可以使用 FireFox 插件 SQLite 检查您的本地数据库。但是,有时在检索时我们会遇到奇怪的问题,例如存储中的内容不正确,有时还会出现崩溃。所以我的建议是你应该检查编码方案(通常,这并不重要)并且在获取数据时使用这个:

[NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(statement, 4)] ;

代替:

[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 4)];

希望,这就是您要找的。任何问题都可以回复我。 :)

【讨论】:

    猜你喜欢
    • 2011-02-15
    • 2019-08-17
    • 2015-09-06
    • 2016-04-23
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多