BLOB数据没有显示的UITextView
我在它的最后一部分stucked,请帮我就可以了。
在这个有三个视图,第一之一,书籍(在的UITableView)名单,第二是章(在的UITableView)和最后一个是对于内容,这是文本格式(在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从第一视图和chapNo来选择在第二视图行。
内容表的样子:
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...>
这里,文本内容我需要显示在文本视图。我在这里错过了什么?
我觉得问题是NSData到NSString的转换。如果你的Sqlite字符串不是以null结尾的话,那么使用下面的行进行转换会有所帮助
NSString* nStr = [[NSString alloc] initWithData:cData
encoding:NSUTF8StringEncoding];
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], &database)==SQLITE_OK)
{
if(sqlite3_prepare_v2(database, query, -1, &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;
}
请执行以下代码,让我通知genrated任何问题和查询。
如果您还想麻烦,请访问我的博客文章.http: //goo.gl/5Q9zs –
感谢您的回应,我试过这个代码...同一概率,没有任何显示错误是新的...问题是与准备stmt:绑定或列索引超出范围 – mavericks
你能否请启用僵尸? –
我找到了最简单的替代方式。
因为,我从数据库中取书名(从第一个表视图)和章节号(从第二个表视图)并将它传递给详细信息视图页面。
比方说在这里,我的书名数学,物理,geography..and章节中的数字(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会有所帮助,如果你想只经过数据库。
感谢您的回复,我已经在使用此之前尝试过(NSString * nstr = [NSString stringWithUTF8String:[cData bytes]];)line..Didn't work out。 – mavericks
不是。有显着差异。如果字符串为空,则应使用UTF8String。否则,我提到的.Sqlite可能不会终止。 – Vignesh
此处cData包含nos序列 mavericks