リストビューからデータベースにデータを保存する方法の例を探しています。
私はいくつかのデータを含むリストビューを持っています:
およびデータベースmysql:
ID、名前、位置、販売、日付
誰かがこれを行う方法の例を教えてもらえますか?
どうも。
リストビューからデータベースにデータを保存する方法の例を探しています。
私はいくつかのデータを含むリストビューを持っています:
およびデータベースmysql:
ID、名前、位置、販売、日付
誰かがこれを行う方法の例を教えてもらえますか?
どうも。
TUNIQuery に ExecSql メソッドがあるかどうかはわかりませんが、これは TADOQuery で機能します。私の場合、ListView.ViewStyle は vsReport に設定され、4 つの列が含まれています。
StringGrid または Dbgrid を使用すると、取り扱いがはるかに簡単になると思います
procedure TForm1.PostData;
const
SQLCMD = 'INSERT INTO MYTABLE (NAME, POSITION, SALL, DATE) VALUES '+
'(%s, %s, %s, %s)';
var
// IL: TListItem;
I, J, ItemsCount, SubItemsCount: integer;
LineItem: array of string;
begin
ItemsCount:= ListView1.Items.Count;
for I := 0 to ItemsCount - 1 do // looping thru the items
begin
SubItemsCount:= ListView1.Items[I].SubItems.count;
SetLength(LineItem, SubItemsCount + 1);
LineItem[0]:= ListView1.Items[0].Caption; // the first item caption (first col)
for J := 0 to SubItemsCount - 1 do // looping thru the subitems of each line
LineItem[J+1]:= ListView1.Items[I].SubItems.Strings[J];
//
// just to see the sql command
// ShowMessage(
// Format(SQLCMD, [ QuotedStr(LineItem[0]),
// QuotedStr(LineItem[1]),
// LineItem[2], //int field no need to quote the parameter
// QuotedStr(LineItem[3])]
// ));
//
with TAdoQuery.Create(nil) do
try
ConnectionString:= 'Your Connection String';
SQL.Text:=
Format(SQLCMD, [QuotedStr(LineItem[0]),
QuotedStr(LineItem[1]),
LineItem[2], //int field no need to quote the parameter
QuotedStr(LineItem[3]));
ExecSql; // you might handel execsql to know the row was affected, also not sure if unidac have the same method
finally
Free;
end;
SetLength(LineItem, 0);
end;
end;
次のソリューションでは、TSQLQuery を使用しています。つまり、Firebird に接続しています。同じ結果が得られる他のクエリ コンポーネントがあると確信しています。
with dstlist do // this is the list view
for i:= 1 to items.count do
with qInsert do // this is the query component
begin
dstlist.itemindex:= i - 1;
lvitem:= dstlist.selected; // select the correct node
close;
parambyname ('p1').asstring:= lvitem.caption; // name
parambyname ('p2').asstring:= lvitem.subitems[0]; // position
parambyname ('p3').asinteger:= strtoint (lvitem.subitems[1]); // sall
parambyname ('p4').asdate:= strtodate (lvitem.subitems[2]);
execsql;
end;
クエリ自体は次のようになります
insert into table (name, position, sall, adate)
values (:p1, :p2, :p3, :p4)