Delphi - 检查数据库中是否已存在记录
Delphi - Check if a record already exists in the database
我正在尝试将一个cxGrid的产品与已经注册到基地的产品进行比较,但我有疑问。
我有以下表格:
产品:id (pk)、描述、参考
items_note: 产品 (pk), 描述, 单位
尝试在事件 cxGridProdutoCustomDrawCell 中执行,但它不起作用
procedure TFrmCompra.cxGridProdutoCustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean);
begin
if QryItemNota.fieldByName('PRODUTO').asString <> QryProduto.fieldByName('REFERENCIA').asString then
begin
ACanvas.Font.Color := clRed
end
else
ACanvas.Font.Color := clBlack;
end;
已使用 Delphi RIO 10.3,带有 Firedac 组件的 Firebird 数据库
很遗憾,您实际上没有说单元格中的文本是用设置为红色还是黑色的字体绘制的。它应该工作正常,所以如果它只为你使用黑色或红色,你的 if
测试就出错了。首先,暂时将您的代码更改为
procedure TFrmCompra.cxGridProdutoCustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean);
begin
ACanvas.Font.Color := clRed
end;
并确保文本确实是红色的。然后,暂时将您的代码更改为
procedure TFrmCompra.cxGridProdutoCustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean);
var
S1,
S2 : String;
begin
S1 := QryItemNota.fieldByName('PRODUTO').asString;
S2 := QryProduto.fieldByName('REFERENCIA').asString;
if S1 <> S2 then
begin
ACanvas.Font.Color := clRed
end
else
ACanvas.Font.Color := clBlack;
end;
然后,在 if S1 <> S2 then
行和 运行 程序上放置一个断点,直到断点命中。使用 F7 计算 S1 和 S2,您应该立即明白为什么没有得到您期望的结果。 S1 和 S2 很可能 总是 相等或 从不 相等。
您没有包含的另一条信息是,这两个查询中的哪一个提供了网格视图,哪个是您检查它的对象。除非您知道其他查询与显示的查询正确同步,否则您应该做类似
的事情
if QueryB.Locate({query B field name}, QueryA.FieldByName({query A field name}).AsString, []) then
// take record found action
else
// take record not found action
这将检查 QueryA 中当前行的值(正在 CustomDraw 事件中处理)是否可以在 QueryB 中找到。
我正在尝试将一个cxGrid的产品与已经注册到基地的产品进行比较,但我有疑问。
我有以下表格:
产品:id (pk)、描述、参考
items_note: 产品 (pk), 描述, 单位
尝试在事件 cxGridProdutoCustomDrawCell 中执行,但它不起作用
procedure TFrmCompra.cxGridProdutoCustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean);
begin
if QryItemNota.fieldByName('PRODUTO').asString <> QryProduto.fieldByName('REFERENCIA').asString then
begin
ACanvas.Font.Color := clRed
end
else
ACanvas.Font.Color := clBlack;
end;
已使用 Delphi RIO 10.3,带有 Firedac 组件的 Firebird 数据库
很遗憾,您实际上没有说单元格中的文本是用设置为红色还是黑色的字体绘制的。它应该工作正常,所以如果它只为你使用黑色或红色,你的 if
测试就出错了。首先,暂时将您的代码更改为
procedure TFrmCompra.cxGridProdutoCustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean);
begin
ACanvas.Font.Color := clRed
end;
并确保文本确实是红色的。然后,暂时将您的代码更改为
procedure TFrmCompra.cxGridProdutoCustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
var ADone: Boolean);
var
S1,
S2 : String;
begin
S1 := QryItemNota.fieldByName('PRODUTO').asString;
S2 := QryProduto.fieldByName('REFERENCIA').asString;
if S1 <> S2 then
begin
ACanvas.Font.Color := clRed
end
else
ACanvas.Font.Color := clBlack;
end;
然后,在 if S1 <> S2 then
行和 运行 程序上放置一个断点,直到断点命中。使用 F7 计算 S1 和 S2,您应该立即明白为什么没有得到您期望的结果。 S1 和 S2 很可能 总是 相等或 从不 相等。
您没有包含的另一条信息是,这两个查询中的哪一个提供了网格视图,哪个是您检查它的对象。除非您知道其他查询与显示的查询正确同步,否则您应该做类似
的事情if QueryB.Locate({query B field name}, QueryA.FieldByName({query A field name}).AsString, []) then
// take record found action
else
// take record not found action
这将检查 QueryA 中当前行的值(正在 CustomDraw 事件中处理)是否可以在 QueryB 中找到。