2

我正在通过 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

索引的字段:

ColumnName - ID CaseInsensitive - False ASC - True MaxIndexSize - 20

4

3 回答 3

2

If you know the primary keys of all the records to be deleted, then you can use one SQL query statement in order to delete all the selected records in one go -

delete from table
where id in (1, 7, 15, 23, 45);

You would have to build this query manually, i.e. create the string which holds the id numbers.

于 2012-10-19T04:21:29.457 回答
0

Answer to own question... I remove the selected records with the code below:

procedure TMain.Button5Click(Sender: TObject);
var
  i: Integer;
begin
  with DBAdvListView1 do
    for i := 0 to Items.Count - 1 do
      if Items[i].Selected then
      begin
        Memo1.Lines.Add(Items[i].Caption + ' - Selected!'); //Test for Correct ID's!
      if dbTable.Locate('ID', Items[i].Caption, []) then
      DBAdvListView1.Datasource.DataSet.Delete;
    end;
  dbTable.Close;
  dbTable.Open;
end;
于 2012-10-21T21:17:35.607 回答
0

second code will work if you just remove the Next; from the if section. Since u're already inside a 'for' loop, there's no need to use the Database.Next.

于 2014-11-03T11:17:52.537 回答