2011-03-29 177 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