开发者

query sqlite3 database multiple tables

In the example below uses a single database table (Animals), which contains three columns (name, description, photo).

If my database contains two tables (1.animals, 2.Cities), each with its columns (name, description, photo), how do I ask for eg. "names Cities", which is positioned in column 1 table 2 .,in combination 开发者_StackOverflow社区with "animal description",positioned in table 1 column 2,to make an object with these "values".

referncing this.....(char *)sqlite3_column_text(compiledStatement, 1)....

Thank you.

    // 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 animals";
    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 *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

            // Create a new animal object with the data from the database
            Animal *animal = [[Animal alloc] initWithName:aName description:aDescription url:aImageUrl];


If the columns of both tables are name, description and photo, I don't see a relationship between them. Anyway, here's an SQL statement that does what you're asking for, but it doesn't make much sense:

SELECT c.name, a.description FROM Cities AS c, animals AS a;

If I have misunderstood your question let me know.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜