我查询资产编号是否在数据库中,它返回false,因此我想将数据添加到数据库,我没有错误。但它从不添加数据。
//check if its there, if so update it, if not add it.
if fdeptlayout.adoquery1.IsEmpty then
begin
showmessage('adding new machine to db');
with Fdeptlayout.ADOQuery1 do
begin
sql.Clear;
sql.BeginUpdate;
sql.add('INSERT INTO MList ');
sql.Add('(Zone,Dept,Number,Name,Asset,IsPanel');
sql.Add(',FinalLocation,Left,Top) ');
sql.Add('VALUES ( :Zone, :Dept, :Number, :Name, :Asset, :IsPanel');
sql.Add(', :FinalLocation, :Left, :Top)');
sql.EndUpdate;
Parameters.ParamByName('Zone').Value := CZone;
Parameters.ParamByName('Dept').Value := CDept;
Parameters.ParamByName('Number').Value := CNumber;
Parameters.ParamByName('Name').Value := CName;
Parameters.ParamByName('Asset').Value := CAsset;
Parameters.ParamByName('IsPanel').Value := CIsPanel;
Parameters.ParamByName('FinalLocation').Value := CFinalLocation;
Parameters.ParamByName('Left').Value := CLeft;
Parameters.ParamByName('Top').Value := CTop;
open;
答案 0 :(得分:10)
您似乎正在使用adoquery1
来获取数据并发送命令。
你不能这样做。
您可以将数据直接附加到您的开放数据集
with Fdeptlayout.ADOQuery1 do
begin
// append a new row
Append;
// set data to all fields
FieldByName('Zone').Value := CZone;
FieldByName('Dept').Value := CDept;
FieldByName('Number').Value := CNumber;
FieldByName('Name').Value := CName;
FieldByName('Asset').Value := CAsset;
FieldByName('IsPanel').Value := CIsPanel;
FieldByName('FinalLocation').Value := CFinalLocation;
FieldByName('Left').Value := CLeft;
FieldByName('Top').Value := CTop;
// post the new added data to the dataset
Post;
end;
您必须使用单独的查询实例来执行sql命令
with TADOQuery.Create( nil ) do
try
// Prepare
Connection := Fdeptlayout.ADOQuery1.Connection;
sql.add('INSERT INTO MList ');
sql.Add('(Zone,Dept,Number,Name,Asset,IsPanel');
sql.Add(',FinalLocation,Left,Top) ');
sql.Add('VALUES ( :Zone, :Dept, :Number, :Name, :Asset, :IsPanel');
sql.Add(', :FinalLocation, :Left, :Top)');
Parameters.ParamByName('Zone').Value := CZone;
Parameters.ParamByName('Dept').Value := CDept;
Parameters.ParamByName('Number').Value := CNumber;
Parameters.ParamByName('Name').Value := CName;
Parameters.ParamByName('Asset').Value := CAsset;
Parameters.ParamByName('IsPanel').Value := CIsPanel;
Parameters.ParamByName('FinalLocation').Value := CFinalLocation;
Parameters.ParamByName('Left').Value := CLeft;
Parameters.ParamByName('Top').Value := CTop;
// Execute the statement
ExecSQL;
finally
Free;
end;
// Update the data
Fdeptlayout.ADOQuery1.Refresh;
// or even better
Fdeptlayout.ADOQuery1.Requery;
答案 1 :(得分:3)
您忘记执行查询:
sql.ExecSQL
更新可能被解释为Self.Update,即表单的可视更新。