【发布时间】:2012-10-17 16:48:27
【问题描述】:
我正在通过 ComponentAce 测试绝对数据库
我的表单上有一个 TABSTable、TABSDatabase 和一个 TDataSource,并且数据显示在 TDBAdvListView 中,MultiSelect 和 RowSelect 为 True。我只有一张桌子。
When either one or more of the Items in the TDBAdvListView are selected I want to have the Database Delete the selected Records.
我在下面的代码中尝试过这种方式:
procedure TMain.DeleteEntry2Click(Sender: TObject);
var
i: Integer;
begin
with DBAdvListView1.DataSource.DataSet do
begin
for i := DBAdvListView1.Items.Count - 1 downto 0 do begin
if DBAdvListView1.Items[i].Selected then
begin
DBAdvListView1.DataSource.DataSet.GotoBookmark(Pointer(DBAdvListView1.Items[i]));
DBAdvListView1.DataSource.DataSet.Delete;
end;
end;
end;
end;
这总是会导致错误消息:
Cannot retrieve record - Native error: 10026
我对数据库编程的经验很少,我做错了什么?
编辑:
我在数据库中添加了一个名为 ID 作为从 0 开始的整数的新字段,希望我可以使用 Locate 方法引用它们并尝试使用下面的代码。这不会产生错误,但只会删除 ListView 中的顶部记录,如果我选择多个记录,它将删除与所选记录不同的记录。
我的新代码:
procedure TMain.DeleteEntry2Click(Sender: TObject);
var
i: Integer;
begin
with DBAdvListView1.DataSource.DataSet do
begin
DBAdvListView1.BeginUpdate;
First;
for i := DBAdvListView1.Items.Count - 1 downto 0 do begin
if DBAdvListView1.Items[i].Selected then
begin
if dbTable.Locate('ID',DBAdvListView1.Items[i].Selected,[]) then
dbTable.Delete;
Next;
end;
end;
dbTable.Close;
dbTable.Open;
DBAdvListView1.EndUpdate;
end;
end;
由于某些奇怪的原因,必须关闭并打开 dbTable 才能查看更改 - 我尝试刷新无济于事...
编辑:
// 根据要求包含表结构...
- ID整数0
- 标题字符串 200
- 作者字符串 100
- 日期字符串 20
- 位置字符串 60
- 类别字符串 100
- ISBN-13 字符串 20
- ISBN-10 字符串 20
在 Absolute Database Utils 目录中有一个 DatabaseManager.exe,我用它来创建实际的表,在这里我现在还设置了一个主键类型:
类型 - 主要 姓名 - ID
索引的字段:
列名 - ID 不区分大小写 - 错误 ASC - 真 MaxIndexSize - 20
【问题讨论】:
-
报错信息是什么意思?
-
@No'amNewman - 我不再收到此错误消息 - 我已更改我的代码,我将作为编辑发布。
-
您应该在表中添加一个主键(例如
ID)。那么你的第一个代码应该可以工作。 -
@Kobik - 谢谢你,我现在就试试 :)
-
@Shambhala 经过几次试验都没有成功我认为你的第一个代码应该可以工作,但我们肯定错过了一点。您能否粘贴您的表结构,以便我们使用 TQuery 发送 DELETE 语句。我认为 GotoBookmark(Pointer(DBAdvListView1.Items[i])) 部分有问题。
标签: database delphi delphi-xe2