SQlite查询问题,导入多条记录

时间:2010-03-02 05:26:59

标签: iphone objective-c database sqlite

好的,所以我在上一期的问题上取得了进展。我现在的问题要简单得多,或者至少我更清楚地了解我的问题是什么。在我的AppDelegate中,我有这段代码从我的数据库中读取数据:

-(void) readFromDatabase {
// Setup the database object
sqlite3 *database;

// Init the Array
sections = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select * from books";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aChapter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *aSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];



            // Create a new object with the data from the database
            Text *text = [[Text alloc] initWithChapter:aChapter verse:aSection];

            // Add the object to the  Array
            [sections addObject:text];

            [text release];
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

这很好用。它正在从books表中读取我的id列之后的两列中的值(这是一个主键)。因此,在应用程序中,第一个值是填充uitableview,按下该行会将您带到详细信息视图,其中值来自db中的下一列。这一切都很好,但我的问题是如何一次导入多个记录。所以在我的详细视图中,我需要有一个uitextview,我需要以某种方式将我的sql查询更改为类似

"select * from books WHERE column = X"

其中X是一个设定数字,我将应用于我想在uitextview中一起显示的一批记录。我不能那样做吗?从不一定唯一的列或主键绘制?那批记录当然会与uitableviewcell中的原始值相关联。我知道这是一件非常简单易行的事情,我只是想不通怎么做!因此,如果有人能够理解这一点并帮助我,那将非常感激!感谢

修改:添加

好的,我遇到的问题,(这可能只是我的数据库的结构或其他)是这一个查询适合所有方法只是不为我做。在我的应用程序中,我需要有1个uitableview,其中填充了数据库中的表中的数据,然后还有一个tableview从那里向下钻取,表示特定于第一个uitablevew中单击的部分的章节编号,这也是数据库中的另一个表,最后是一个在uitextview中同时显示多个记录的详细视图,所有这些都处理第二个uitableview中选择的章节,再次来自数据库中的另一个表。所以我的问题是,我不喜欢这种方法,我已经把自己放进去了,我在做一个查询说“选择*,只是得到你发现的下两个值”这样的事情。我需要能够使我的查询更具体,而不是仅在一个表中,而是多个。这是否意味着我可以设置多个const char *sqlStatement =语句并将它们专门链接到我需要的所需输出?如果您能够展示这些部件如何组合在一起,我们将非常感激。感谢

1 个答案:

答案 0 :(得分:1)

更改以上查询

const char * sqlStatement =“select * from books”;

像这样

const char * sqlStatement = [[NSString stringWithFormat:@“select * from books where column =%@”,X] UTF8String];

如有疑问请回复!