gpt4 book ai didi

ios - 将数据 SQlite 加载到 htmlString iOS

转载 作者:行者123 更新时间:2023-11-29 13:19:18 26 4
gpt4 key购买 nike

我有一个从 sqlite 数据库读取数据的应用程序,使用此方法读取数据并将其包含在对象中....我用 NSLog 进行了检查

#import "ViewController1.h"
#import "Ricetta.h"
#import "AppDelegate.h"


static sqlite3_stmt *leggiStatement = nil;

@interface ViewController1 ()

@end

@implementation ViewController1

@synthesize Webmain, oggetto2;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization



}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.


//percorso file su cartella documents
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"Rice.sqlite"];

//controllo se il file esiste
if(![[NSFileManager defaultManager] fileExistsAtPath:path])
{
//se non esiste lo copio nella cartella documenti
NSString *pathLocale=[[NSBundle mainBundle] pathForResource:@"Rice" ofType:@"sqlite"];
if ([[NSFileManager defaultManager] copyItemAtPath:pathLocale toPath:path error:nil] == YES)
{
NSLog(@"copia eseguita");
}
}





[self personalizzaAspetto];


[self carica_ID];




// NSString * query = @" SELECT Immagine, Titolo, Descrizione FROM LIBRO";
// NSArray * arrayQuery = [[NSArray alloc] initWithObjects:@"Immagine",@"Titolo",@"Descrizione",nil];
// NSArray * arrayElementi = [self caricaValoriMain:query :arrayQuery];




Webmain= [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 365)];
NSString *htmlString =[NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%@\"; font-size: %@;}\n"
"</style> \n"
"</head> \n"
"<body><center><img src='%@'/></center></body><center><h1>%@</h1></center><body bgcolor=\"#FFFFFF\" text=\" #ffa500\">%@</body></html>" ,@"futura",[NSNumber numberWithInt:15],oggetto2.Immagine,oggetto2.Titolo,oggetto2.Descrizione];

[Webmain loadHTMLString:htmlString baseURL:nil];

[self.view addSubview:Webmain];







-(void)carica_ID{




sqlite3 *database = NULL;

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"Rice.sqlite"];

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
if(leggiStatement==nil){
const char *sql = "select Immagine,Titolo,Descrizione from LIBRO WHERE RicettaID=1";

if(sqlite3_prepare_v2(database, sql, -1, &leggiStatement, NULL) != SQLITE_OK)
NSAssert1(0, @"Errore creazione compiledState '%s'", sqlite3_errmsg(database));

}


//while(sqlite3_step(leggiStatement) == SQLITE_ROW)
if(SQLITE_DONE != sqlite3_step(leggiStatement))
{




NSString *titolo = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(leggiStatement, 1)];
NSLog(@"%@",titolo);
oggetto2.Titolo=titolo;

NSString *descrizione = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(leggiStatement, 2)];
NSLog(@"%@",descrizione);
oggetto2.Descrizione = descrizione;

NSString *image= [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(leggiStatement, 0)];
NSLog(@"%@",image);
oggetto2.Immagine= image;



}

sqlite3_finalize(leggiStatement);
}

sqlite3_close(database);

}



@end

我的问题是我无法将它们放入 webMain 中...webMain 中的对象仍然是空的。

我不使用 Xib。

最佳答案

在提供的代码片段中,您从未执行 oggetto2allocinit。因此,它是 nil,因此尝试设置其属性将一事无成。

除了您现有的 NSLog 语句外,我还建议您在执行 之前对 htmlString 执行 NSLog loadHTMLString,因为通过查看源代码可以更轻松地了解您的 HTML 发生了什么,而不是尝试从空白的 Web View 中进行推断。


与您的问题无关,但您可能不应该拥有可能在最终确定后重用静态 sqlite3_stmt 的代码。第一次调用 carica_ID 时,您将初始化静态 leggiStatement。但是您最终执行了 sqlite_finalize,但没有将 leggiStatement 设置为 nil。如果您第二次调用此方法,它不会再次 sqlite3_prepare_v2,但您将释放与之前的 leggiStatement 关联的资源。

几个简单的修复:

  • 不要将 leggiStatement 设为静态全局变量,而是将其设为方法的局部非静态变量;

  • 如果您执行sqlite3_finalize,请确保您也将leggiStatement 设置为nil;或者

  • 不要调用 sqlite3_finalize,而只是调用 sqlite3_reset,这将重置准备好的语句,但不会释放其资源。

关于ios - 将数据 SQlite 加载到 htmlString iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14789550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com