开发者

SQlite query question, importing multiple records

开发者 https://www.devze.com 2022-12-21 20:17 出处:网络
Ok, so I am making progress since my last question. My question now is a lot more simple, or at least I understand more clearly what my question is. In my AppDelegate I have this code to read in data

Ok, so I am making progress since my last question. My question now is a lot more simple, or at least I understand more clearly what my question is. In my AppDelegate I have this code to read in data from my database:

-(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 arr开发者_高级运维ay
        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);

And that is working fine. It is reading in from the books table the values that came from the two columns after my id column (which is a primary key). So in application, the first value is populating a uitableview, and to press the row takes you to the detail view where the value is from the next column in the db. This is all fine, but my question is about how to import multiple records at once. So in my detail view, I need there to be a uitextview, and I need to somehow change my sql query to something like

"select * from books WHERE column = X"

where X would be a set number I would apply to the batch of records I want to have displayed all together in the uitextview. Can't I do that? Draw from a column that isn't necessarily unique, or a primary key? That batch of records would of course be associated with the original value in the uitableviewcell. I know this is a very simple and easy thing to do, I just can't figure out how to do it! So if anyone can make sense of this and help me out, it would be GREATLY appreciated! Thanks

Edit: Addition

Ok so the problem I am running into, (and this may just be the structure of my database or something) is that this one query fits all approach just isnt doing it for me. In my app, I need to have 1 uitableview that is populated with data from a table in my database, and then one more tableview that drils down from there that represents chapter numbers specific to the section clicked on in the first uitablevew, thats also from another table in the database, and then finally a detail view that shows multiple records at the same time in a uitextview, all that deal with the chapter selected in the second uitableview, again, from another table in the database. So my issue is that I'm not liking this approach I have gotten myself into where Im making a query saying "select * and just get the next two values you find" kind of a thing. I need to be able to make my queryies more specific, and not exclusively in one table, but multiple. Does that mean that I can setup multiple const char *sqlStatement = statements and link them specifically to the desired output I need? If you could just kind of show how these pieces fit together, it would be very appreciated. Thanks


change the above query

const char *sqlStatement = "select * from books";

like this

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

if any queries please reply!

0

精彩评论

暂无评论...
验证码 换一张
取 消