Executing Multiple queries
I was following a tutorial. 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""
that's 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 have to create a new method and have to call 开发者_JS百科this new method or what? how can I execute another query?
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.
精彩评论