2013-04-04 20 views
-1

我使用可變table'name數據庫,如:!使用可變table'name在FMDB

BOOL judge=[self.database executeUpdate:@"insert into 
?(id,name,headimg,excerpt,postion) values 
(?,?,?,?,?)",tableName,aID,aName,aImg,aExcept,aCoor]; 

但這種「插入」功能將失效!我不知道該怎麼辦

回答

1

如果你想使用的表名作爲變量,你可以試試下面的

NSString *sql=[NSString stringWithFormat:@"insert into %@ (id,name,headimg,excerpt,postion) values (%@,%@,%@,%@,%@)",tablename,aID,aName,aImg,aExcept,aCoor]; 

BOOL judge=[self.database executeUpdate:sql]; 
+0

tablename不能使用變量嗎? – willBetter 2013-04-04 09:16:42

+0

沒有準備好聲明..但是,如果你想使用tablename作爲變量,你可以創建你的查詢作爲格式化的字符串..這也將工作.. – iphonic 2013-04-04 09:17:59

+0

@willBetter請參閱編輯答案.. – iphonic 2013-04-04 09:21:19

0

試試這個:

NSString *query = [NSString stringWithFormat:@"insert into '%@' (id,name,headimg,excerpt,postion) values (%d,'%@','%@','%@','%@')",tableName,aID,aName,aImg,aExcept,aCoor]; 

BOOL judge=[self.database executeUpdate:query]; 

根據你的需要也改變了格式說明。 (大概是這樣用的ID%d,其他變量作爲字符串,以便用於%@)

+0

我使用這種方法,但它不起作用 – willBetter 2013-04-04 09:56:41

+0

@willBetter:請NSLog查詢並檢查是否正確。 – 2013-04-04 10:04:33

+0

我用這種方法,沒關係!!剛纔,謝謝。我的英文不好 – willBetter 2013-04-04 10:06:33

0

我做它的方式保持SQLite的工作結合:)

NSString * queryString = [NSString stringWithFormat:@"SELECT * FROM %@ ",[self modelDatabaseTableName]]; 
queryString = [queryString stringByAppendingString:@"WHERE UUID = ?;"]; 
FMResultSet *existenceCheckResultSet = [database executeQuery:queryString,[modelObject valueForKey:@"UUID"]]; 

希望它能幫助。

Giova