tags:

views:

17

answers:

1

HI all,

i was folloeing this tutorial Sample code it was working great..

my doubht is, to execute single query they executed in this method

"+ (void) getInitialDataToDisplay:(NSString *)dbPath {"  

as "select coffeeID, coffeeName from coffee""

thats fine. but for my next view if i want execute a new query like "select * from coffee where abc = 123".

where should i write this query?? i hv to create a new method and hv to call this new method or what? how can i execute another query? plz suggest.

+1  A: 

1) If you're going to use sqlite. I suggest you learn how to add Core Data to your Application.

2) To answer your question. You could add a method to the coffee class to retrieve the data you need. It could be a class method implemented just like:

 + (void) getData:(NSString *)dbPath {
    SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    // <---Modify the sqlite statement below
    const char *sql = "select coffeeID, coffeeName from coffee"; 
    sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

    while(sqlite3_step(selectstmt) == SQLITE_ROW) {

    NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
    Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
    coffeeObj.coffeeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; // <-- create objects in coffeeObj that you want to do something with.

    coffeeObj.isDirty = NO;

    [appDelegate.coffeeArray addObject:coffeeObj];
    [coffeeObj release];
    }
    }
    }
    else
    sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

Make sure you add the corresponding method to the .h file as well.

Jordan
Thanks Jordan, just one thing i need to ask..i passed query in it, but how and wheer should i call this "+ (void) getData:(NSString *)dbPath " method????
shishir.bobby
ohk got it running ,i called this method in applicationdidfinish launching. but one thing, i am getting return data as "<Coffee: 0x7515020>" in this format. when i close app and run it again, it shows the real data, do u know why this is happening? thanks a ton sir
shishir.bobby
Coffee is a class. More precisely it is a data model object. It is a structure that holds your data. You reference NSStrings and such using dot notation. For example in the code above the name of the coffee is stored in coffeeObj.coffeeName.
Jordan
so, i am getting returend values as "<Coffee: 0x7515020>", is there any way to get actual value?? thanks again sir
shishir.bobby
Coffee *coffeeObj = [coffeeArray objectAtIndex:0]; NSlog(@"%@",coffeeObj.coffeeName); will retrieve the first object in coffeeArray and output it to the console.
Jordan
yea got it running thanks a ton sir..but my dta is not coming out instantly. i hv to rerun my app to see the data.on first time only data comes up, i can c it no console, but the tablview is not showing up on fist time. than i re run my app and whole data shown up.. wat with this now? i alreday tried [able reload]..but its not working for me.
shishir.bobby
Time to ask another question. Your original question has been answered. Accept the answer and ask another question.
Jordan