我正在搜索如何将数据从列表视图保存到数据库的示例。从列表视图插入数据到数据库delphi
我有一些数据的列表视图:
http://s24.postimage.org/i1rhxnadx/listview.jpg
和mysql数据库:
ID,姓名,职务,自主学习,日期
有人能告诉我一个例子如何做这个?
thx。
我正在搜索如何将数据从列表视图保存到数据库的示例。从列表视图插入数据到数据库delphi
我有一些数据的列表视图:
http://s24.postimage.org/i1rhxnadx/listview.jpg
和mysql数据库:
ID,姓名,职务,自主学习,日期
有人能告诉我一个例子如何做这个?
thx。
不知道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;
感谢您的关注,稍后我会检查并在发生一些变化后回报 – vkatsitadze 2013-03-04 16:53:26
,我有工作来源。谢谢 – vkatsitadze 2013-03-07 15:07:38
以下解决方案使用TSQLQuery,这意味着我连接到火鸟。我相信还有其他的查询组件会给你相同的结果。
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)
什么是你试过吗?你正在使用哪个数据库组件? – RRUZ 2013-03-04 13:51:57
我使用UniDac(devart.com)数据库组件,我无法尝试..因为我无法填写如何开始,你能给我一些提示吗? – vkatsitadze 2013-03-04 14:06:15