iphone
  • ios
  • sqlite3
  • xcode5
  • fmdb
  • 2013-09-30 81 views 0 likes 
    0

    我想從不同的表中獲取結果,表格由用戶選擇。所以,我使用表名作爲變量,但它返回nil查詢。Fmdb選擇具有變量表名稱的查詢

    FMResultSet *query = [db1 executeQuery:@"SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'", [NSString stringWithFormat:@"%@1",tableName], [NSString stringWithFormat:@"%@2",tableName], 
    [NSString stringWithFormat:@"%@3",tableName], [NSString stringWithFormat:@"%@4",tableName], [NSString stringWithFormat:@"%@5",tableName], [NSString stringWithFormat:@"%@6",tableName]]; 
    

    如果我對錶名進行硬編碼,它將返回數據。

    FMResultSet *query = [db1 executeQuery: 
    @"SELECT Image, Explanation FROM Class1 WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM Class2 WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM Class3 WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM Class4 WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM Class5 WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM Class6 WHERE Image !='empty'"]; 
    

    我記錄了tableName值,它返回了正確的值,即「Class」。 請幫我解決這個問題,併爲此提出最佳查詢。

    回答

    0

    使用此代碼:

    FMResultSet *query = [db1 executeQuery:[NSString stringWithFormat:@"SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'" 
    "UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'", [NSString stringWithFormat:@"%@1",tableName], [NSString stringWithFormat:@"%@2",tableName], 
    [NSString stringWithFormat:@"%@3",tableName], [NSString stringWithFormat:@"%@4",tableName], [NSString stringWithFormat:@"%@5",tableName], [NSString stringWithFormat:@"%@6",tableName]]]; 
    
    +0

    感謝@Vin。這是針對此目的的正確/優化查詢嗎? –

    相關問題