I have an app that reads from sqlite database,data is read and included in the objects using this method ….I checked with 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
My problem is that I can not put them in webMain…objects in webMain remain empty.
I do not use Xib.
In the code snippet provided, you never perform the
allocandinitofoggetto2. Thus, it isnil, and thus attempts to set its properties will achieve nothing.In addition to your existing
NSLogstatements, I’d also suggest doing aNSLogof thehtmlStringright before you performloadHTMLString, because it’s easier to see what’s going on with your HTML by looking at the source, rather than trying to make inferences from a blank web view.Unrelated to your problem, but you probably should not have code that could potentially reusing your static
sqlite3_stmtafter you’ve finalized it. The first time you callcarica_IDyou would initialize the staticleggiStatement. But you end up doing asqlite_finalizebut don’t setleggiStatementtonil. If you ever called this method a second time, it won’tsqlite3_prepare_v2again, but you will have freed the resources associated with your priorleggiStatement.A couple of easy fixes:
do not make
leggiStatementa static global, but rather make it a local, non-static variable of the method;if you do
sqlite3_finalize, make sure you setleggiStatementtonilas well; ordon’t call
sqlite3_finalize, but rather just callsqlite3_reset, which will reset the prepared statement, but won’t release its resources.