1

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.

3
  • If you are not using a xib, why did you implement the initWithNibName:bundle: method? Commented Feb 9, 2013 at 19:28
  • I think it is not really implemented... Commented Feb 9, 2013 at 21:40
  • When you say that the web view is empty, do you see anything, such as a broken link and references to "(null)"? Or is it literally empty, completely blank, all white and nothing showing at all? Commented Feb 9, 2013 at 22:18

1 Answer 1

1

In the code snippet provided, you never perform the alloc and init of oggetto2. Thus, it is nil, and thus attempts to set its properties will achieve nothing.

In addition to your existing NSLog statements, I'd also suggest doing a NSLog of the htmlString right before you perform loadHTMLString, 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_stmt after you've finalized it. The first time you call carica_ID you would initialize the static leggiStatement. But you end up doing a sqlite_finalize but don't set leggiStatement to nil. If you ever called this method a second time, it won't sqlite3_prepare_v2 again, but you will have freed the resources associated with your prior leggiStatement.

A couple of easy fixes:

  • do not make leggiStatement a static global, but rather make it a local, non-static variable of the method;

  • if you do sqlite3_finalize, make sure you set leggiStatement to nil as well; or

  • don't call sqlite3_finalize, but rather just call sqlite3_reset, which will reset the prepared statement, but won't release its resources.

Sign up to request clarification or add additional context in comments.

3 Comments

I set sqlite3_finalize to sqlite3_reset, leggiStatement as non static variable... Unfortunately no change...I also set NSlog check before loadHTMLString...object remain empty
@geminimac Have you added the alloc and init of oggetto2? That sqlite3_finalize issue was a secondary issue (though important). It strikes me that oggetto2 was the main issue.
great!! ... you have been wonderful ... you're a genius! I initialize the object like this: oggetto2= [[Ricetta alloc]initWithPrimaryKey:0]....worked!Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.