【问题标题】:BLOB data is not displaying in UITextViewBLOB 数据未在 UITextView 中显示
【发布时间】:2013-05-24 11:52:26
【问题描述】:

你好

我被卡在了最后一部分,请帮助我。

其中有三个视图第一个一个,用于书籍列表(在UITableView中), secondchapters(在 UITableView 中),lastcontent,其中是文本格式(在 UITextView 中)。

到第二个视图它工作得很好。问题是,当我在第二个视图中选择任何章节时,第三个视图中的文本视图 什么都不显示。但是在输出中显示数字序列。

从数据库中获取数据并显示到 UITextView 中的代码。

 NSMutableString *mStr = [NSMutableString stringWithFormat:@"select content from content where name = \"%@\" and chapterno = %@",chapName,chapNo];
 const char *sql =(char *)[mStr UTF8String];

    sqlite3_stmt *sqlStmt;
    if(sqlite3_prepare(dbContent, sql, -1, &sqlStmt, NULL)!=SQLITE_OK)
    {
        NSLog(@"prob with prepare statement: %s",sqlite3_errmsg(dbContent));
    }
    else
    {
        while (sqlite3_step(sqlStmt)==SQLITE_ROW) {
            len = sqlite3_column_bytes(sqlStmt, 0);
            NSData *cData = [[NSData alloc]initWithBytes:sqlite3_column_blob(sqlStmt, 0) length:len];

            NSString *nstr = [NSString stringWithUTF8String:[cData bytes]];
            myTextView.text = nstr;
  }

在上面的 chapName 来自 first view 并且 chapNosecond view 中被选中。 p>

内容表如下:

 id      bookid      name       chapterno          content

  1        1          abc           1            BLOB(size:4217)  
  2        1          abc           2            BLOB(size:3193)
  3        1          abc           3            BLOB(size:3501)

O/p 是这样的一长串数字。

  <0d0a3120 496e2074 68652062 6567696e 6e696e67 20476f64 20637265 61746564 20746865 20686561 76656e20 616e6420 74686520 65617274 682e0d0a...>

在这里,我需要在文本视图中显示文本内容。我在这里缺少什么?

【问题讨论】:

    标签: iphone uitableview sqlite uitextview


    【解决方案1】:

    我认为问题在于 NSData 到 NSString 的转换。如果您的 Sqlite 字符串不是以 null 结尾的,则使用以下行进行转换应该会有所帮助。

    NSString* nStr = [[NSString alloc] initWithData:cData
                                         encoding:NSUTF8StringEncoding];
    

    【讨论】:

    • 感谢您的回复,我在使用 this(NSString *nstr = [NSString stringWithUTF8String:[cData bytes]];)line 之前已经尝试过了。。没有成功。
    • 没有。有显着差异。如果字符串以 null 结尾,则应使用 UTF8String。否则我提到的那个。Sqlite 不能为空终止。
    • 此处CDATA包含号
    【解决方案2】:
      NSMutableArray *ar=[[NSMutableArray alloc] init];
     NSString *strQuery=[NSString stringWithFormat:@"select content from content where name = \"%@\" and chapterno = %@",chapName,chapNo];
     const char *sql =[mStr UTF8String];
    
        sqlite3_stmt *sqlStmt;
        if(sqlite3_open([self.dbPath UTF8String], &amp;database)==SQLITE_OK) 
        {
            if(sqlite3_prepare_v2(database, query, -1, &amp;compiledStmt, NULL)==SQLITE_OK){
             while (sqlite3_step(sqlStmt)==SQLITE_ROW) {
                len = sqlite3_column_bytes(sqlStmt, 0);
                NSData *cData = [[NSData alloc]initWithBytes:sqlite3_column_blob(sqlStmt, 0) length:len];
    
                NSString *nstr = [NSString stringWithUTF8String:[cData bytes]];
                myTextView.text = nstr;
    
             [ar addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSData dataWithBytes:sqlite3_column_blob(compiledStmt, 1) length:sqlite3_column_bytes(compiledStmt, 1)],@"data1",
                                      [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStmt, 2)],@"data2",nil]];
        }
        else {
                    NSLog(@"Invalid query");
                }
     NSArray *arToReturn=([ar count]>;0)?[NSArray arrayWithArray:ar]:nil;
            ar=nil;
    return arToReturn;
        }
    

    请执行以下代码,让我告知任何问题和生成的查询。

    【讨论】:

    • 如果您还想遇到麻烦,请访问我的博文。goo.gl/5Q9zs
    • 感谢您的回复,我尝试了此代码..相同的概率,没有显示错误是新的...概率是准备 stmt:bind 或列索引超出范围..
    • 可以请你启用僵尸吗?
    • "[ar oddobj...] 给出的 bind 或 col 索引超出范围...并且 nStr 具有空值。
    • 是否完整地关注我的博文?
    【解决方案3】:

    我找到了一个最简单的替代方法。

    因为,我从数据库中获取书名(来自第一个表视图)和章号(来自第二个表视图)并将其传递到详细视图页面。

    在这里说一下,我的书名是数学、物理、地理......章节是数字(1、2、3......)。只需将文件名保持为

      [booknamechpatername.txt]
    #book name- Maths
      chapter1-->> maths1.txt
      chapter2-->> maths2.txt
      ...
    #book name - Physics
      chapter1 -->>  physics12.txt
      ...
      geography21.txt
     ...so on..
    

    并将其拖到 iphone 资源或您想要的任何文件夹。 现在,将该文件存储到字符串中并显示到 UITextView 中。

    -(void)loadFile{
       NSError *error;
       NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
       NSString *docDir = [dirPath objectAtIndex:0];
       if (!docDir) {
           NSLog(@"Doc dir not found");
        }
       NSString *str = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@%@",chapName,chapNo] ofType:@"txt"];
    
       NSString *content = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:&error];
       myTextView.text = content;
    }
    

    并在 viewDidLoad

    中调用它
     [self loadFile];
    

    否则,如果您只想通过数据库,此链接 https://github.com/jblough/bible-ios 会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多