|
SQL 語(yǔ)句的拼接,關(guān)鍵點(diǎn)在于對(duì)引號(hào)的處理上。 在 delphi 的語(yǔ)法中,使用單引號(hào)做字符串的標(biāo)志符。因此,當(dāng)遇到 SQL 語(yǔ)句中字符串標(biāo)識(shí)量編寫的時(shí)候,需要用兩個(gè)單引號(hào)來(lái)代替實(shí)際的引號(hào)。 舉例: SQL := 'Insert into table(表名) values(' + ''''數(shù)據(jù)值1'''' + ',' + ''''數(shù)據(jù)值2'''' + ',' + ''''數(shù)據(jù)值'''' + ')'; 看起來(lái)單引號(hào)太多,顯得挺亂。 其實(shí)可以用其他手段來(lái)完成: 1、使用 QuotedStr 函數(shù) SQL := 'Insert into table(表名) values(' + QuoteStr('數(shù)據(jù)值1') + ','+ QuotedStr('數(shù)據(jù)值2') + ','+ QuotedStr('數(shù)據(jù)值3') + ')'; 2、使用 Format 函數(shù) SQL := Format('Insert into table(表名) values(%s,%s,%s)', [QuoteStr('數(shù)據(jù)值1''), QuotedStr('數(shù)據(jù)值2), QuotedStr('數(shù)據(jù)值3')]);
|
|
|