updateSql 這個控制項在我剛學 delphi 時就知道了,但不直不會用,上網也沒有找到過例子,今天正好需要用到這
個控制項,一用,還真好用,呵呵,說一下它的使用方法吧
updateSql 這個控制項原本是用來更新不同的表的,當用 query 開啟了多個表,但這時就不能直接用 query 來更新
表的操作了,因為裡面是多個表,這裡就用 updateSql 這個控制項了,把 query 的 updatObject 指向一個
updateSql,同時這個 query 的 cacheUpdates 為 true,我試著用 false 試了試,在 dbgrid 裡不能輸入資料。
我這裡query 只開啟了一張表,開始試著把 RequestLive 設為 true ,但 open 時,提示 can not find object
上網搜搜是什麼意思,但沒有找到答案,國外的網站提到這個問題,是說 sql 資料庫中用了 Nchar,或 nvarchar
資料類型,但我沒有用呀,不知為什麼,於是就想到了 updateSql ,還沒用過呢,先試試吧,設好屬性後, open
,沒有提示了,好,改變資料 再 applyUpdates ,出錯了,因為沒有 modifySql ,但這個 modifySql 該怎麼寫
呢?查了查 線上說明
Specifies the SQL UPDATE statement to use when applying an update to a record and cached updates is
enabled.
property ModifySQL: TStrings;
Description
Set ModifySQL to the SQL UPDATE statement to use when applying an updated record to a dataset.
Statements can be parameterized queries. To create a UPDATE statement at design time, use the
UpdateSQL editor to create statements, such as:
UPDATE "Country.db"
SET Name = :Name, Capital = :Capital, Continent = :Continent
WHERE Name = :OLD_Name
At run time, an application can write a statement directly to this property to set or change the
UPDATE statement.
Note: As the example illustrates, ModifySQL supports an extension to normal parameter binding. To
retrieve the value of a field as it exists prior to application of cached updates, the field name
with “OLD_? ”This is especially useful when doing field comparisons in the WHERE clause of the
statement.
看得雖不太明白,但也明白點了,就是更新表時,用 欄位 =:欄位 但條件呢 也就是 where 該怎麼寫呢
它在下面說了 用 OLD_欄位名,按它說的做做 , applyUpdates ,沒有錯,再開啟資料庫看看,哎,資料更新了
到這裡,我知道該怎麼用 updateSql 了
注意一下的是
寫 sql 語句時 如 set name =: name , sex =: sex 這樣就不可以,必須去掉空格,要不會出現 Field '' is of
an unknown type 的提示,不知為什麼它不會自已去掉這些空格