2010-10-14 128 views
0

大家好,执行多个查询

我是folloeing本教程 Sample code 它是伟大的工作..

我doubht是,执行他们在这个方法中执行一个查询

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

as "select coffeeID, coffeeName from coffee""

那很好。但对于我的下一个视图,如果我想执行一个新的查询,如“select * from coffee where abc = 123”。

我应该在哪里写这个查询?我想创建一个新的方法,并呼吁这个新的方法或什么? 我该如何执行另一个查询? PLZ建议。

回答

1

1)如果你打算使用SQLite。我建议你学习如何将Core Data添加到你的应用程序中。

2)要回答你的问题。你可以添加一个方法到咖啡类来检索你需要的数据。这可能是实现一个类的方法就像:

+ (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. 
    } 

请务必添加相应的方法来.h文件也是如此。

+0

感谢乔丹,只有一件事我需要ask..i通过它查询,但如何和wheer我应该称之为“+(无效)的getData:(的NSString *)DBPATH”方法???? – iscavengers 2010-10-14 06:52:54

+0

ohk运行了,我在applicationdidfinish启动时调用了这个方法。但有一点,我以这种格式获取返回数据为“”。当我关闭应用程序并再次运行它时,它会显示真实的数据,你知道为什么会发生这种情况吗?谢谢一吨先生 – iscavengers 2010-10-14 06:57:40

+0

咖啡是一个类。更确切地说,它是一个数据模型对象。这是一个保存你的数据的结构。您使用点符号引用NSStrings等。例如,在上面的代码中,咖啡的名称存储在coffeeObj.coffeeName中。 – Jordan 2010-10-14 07:02:38