2011-03-29 180 views
0

在下面的例子中使用了一个单一的数据库表(动物),其中包含三列(名称,描述,照片)。查询sqlite3数据库多表

如果我的数据库包含两个表(1.animals,2.Cities),每个表都有它的列(名称,描述,照片),我该如何要求例如。 “名称城市”定位在第1列表2中,并结合表1第2列中的“动物描述”,用这些“值”来制作对象。

referncing这.....(字符*)sqlite3_column_text(compiledStatement,1)....

谢谢。

// 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]; 
+0

在你的问题旁边,使用fmdb,一个很棒的SQLite3 Objective-C包装器。 https://github.com/ccgus/fmdb – 2011-03-30 00:29:02

+0

@ Cesar A. Rivas谢谢 – Xzolo32 2011-03-30 16:05:36

回答

0

如果两个表的列是namedescriptionphoto,我不认为他们之间的关系。总之,这里是一个SQL语句,做你问什么,但它并没有多大意义:

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

如果我误解了你的问题让我知道。

+0

你理解我的问题。 我重建了这个问题才有意义。 表1(动物)列(1name,2description,3photo)和表2(城市)列(1hotels,2bars,3pubs)。 请给我这个查询。 并告诉我应该为3pubs或1hotels而不是x放置什么数字: (char *)sqlite3_column_text(compiledStatement,x) 非常感谢。 – Xzolo32 2011-03-30 00:39:13