SQL查询更新然后还原更改

时间:2011-10-17 10:44:12

标签: sql delphi ms-access delphi-7 tadoquery

我目前对这个问题感到困惑。我可以插入和删除表中的记录,但我无法更新某些字段。它会在0.5秒后恢复更改之前暂时更新它,我实际上看到了更改。顺便说一下,这是在Delphi 7中完成的:

CloseDatabase;     // Closes my database first to prevent an error from accessing one that is already open
OpenDatabase;      // Dynamically opens the database
ActivateEdits;

if dbeEnglish.Enabled then
begin
  qryDictionary.SQL.Text := 'Update [word list] set [english] = "'+dbeEnglish.Text+'" where  ([afrikaans] = "'+dbeAfrikaans.Text+'") and ([english] = "'+sEnglishBefore+'")';
  qryDictionary.ExecSQL;
end
else
begin
  qryDictionary.SQL.Text := 'Update [word list] set [afrikaans] = "'+dbeAfrikaans.Text+'" where ([english] = "'+dbeEnglish.Text+'") and ([afrikaans] = "'+sAfrikaansBefore+'")';
  qryDictionary.ExecSQL;
end;

SelectAll;          // SQL to select * from [word list]  as well as set the column widths
bEngOnce := False;  // variable i used to prevent both dbe (data base edits) from being edited
bAfrOnce := False;

我是否在OI中更新错误或丢失了某些内容?它确实更新不会使它永久化。

忘记提及:表格单词列表有3个字段:一个名为ID,英语和南非荷兰语的自动编号字段。自动编号是否会导致更新问题?

1 个答案:

答案 0 :(得分:1)

我会尝试以下方法。我不确定它是否有帮助,但您可以检查ExecSQL结果。似乎Bharat提到您的代码中存在未经授权的事务。

...
if dbeEnglish.Enabled then
begin
  qryDictionary.Connection.BeginTrans;
  try
    qryDictionary.SQL.Text := 'Update [word list] set [english] = "'+dbeEnglish.Text+'" where  ([afrikaans] = "'+dbeAfrikaans.Text+'") and ([english] = "'+sEnglishBefore+'")';
    qryDictionary.ExecSQL;
    qryDictionary.Connection.CommitTrans;
  except
    qryDictionary.Connection.RollbackTrans;
  end;
end
else
begin
  qryDictionary.Connection.BeginTrans;
  try
    qryDictionary.SQL.Text := 'Update [word list] set [afrikaans] = "'+dbeAfrikaans.Text+'" where ([english] = "'+dbeEnglish.Text+'") and ([afrikaans] = "'+sAfrikaansBefore+'")';
    qryDictionary.ExecSQL;
    qryDictionary.Connection.CommitTrans;
  except
    qryDictionary.Connection.RollbackTrans;
  end;
end;
...

您还可以检查某些行是否会受到提交的影响。这是由TADOQuery.ExecSQL函数结果返回的,所以你可以这样检查它。

var
  RowsAffected: Integer;
...
RowsAffected := qryDictionary.ExecSQL;
ShowMessage(IntToStr(RowsAffected) + ' row(s) will be affected by commiting this query ...');
...