English 中文(简体)
Delphi TAdoQuery-多个插入?
原标题:Delphi TAdoQuery - Multiple Inserts?
  • 时间:2010-10-16 04:44:14
  •  标签:
  • delphi

Delphi TAdoQuery是否可以一次执行多个插入,或者必须单独执行每个语句?我想做的是:

AdoQuery.SQL.Clear;
AdoQuery.SQL.Add( INSERT INTO user VALUES (1, "User 1"); );
AdoQuery.SQL.Add( INSERT INTO user VALUES (2, "User 2"); );
AdoQuery.SQL.Add( INSERT INTO user VALUES (3, "User 3"); );
AdoQuery.ExecSQL;   
AdoQuery.Close;

这可能吗?我在执行这个时从MySQL中得到一个错误。我还尝试添加BEGIN;和END;绕过查询,但这也不起作用。

编辑:我之所以想这样做,是因为当我在for循环中执行插入时,似乎需要很长时间才能执行>;10个查询。我想把它们都像上面那样加起来会加快速度。有人知道在插入之间是否需要AdoQuery.Close调用吗?

最佳回答

使用MySQL,您可以使用以下语法:

INSERT INTO user VALUES (1, "User 1"), (2, "User 2"), (3, "User 3")

然后您可以使用参数:

AdoQuery.SQL.Text :=  INSERT INTO user VALUES (:p11, :p12), (:p21, :p22), (:p31, :p32) ;
AdoQuery.Parameters[0].Value := 1;
AdoQuery.Parameters[1].Value :=  User 1 ;
AdoQuery.Parameters[2].Value := 2;
AdoQuery.Parameters[3].Value :=  User 2 ;
AdoQuery.Parameters[4].Value := 3;
AdoQuery.Parameters[5].Value :=  User 3 ;
AdoQuery.ExecSQL;
问题回答

尝试这样的操作(使用AdoCommand):

sSql :=  INSERT INTO User (FieldName1, FieldName2) values (:Nr, :Strng) ;
AdoCmd.Parameters.Clear();     
AdoCmd.CommandText := sSql;
AdoCmd.CommandType := cmdText;
AdoCmd.Parameters.ParseSQL( sSql, True );
AdoCmd.Parameters.ParamByName( Nr ).DataType := ftInteger
AdoCmd.Parameters.ParamByName( Strng ).DataType := ftString;

for i := 1 to 10 do
begin     
    AdoCmd.Parameters.ParamByName( Nr ).Value := i;
    AdoCmd.Parameters.ParamByName( Strng ).Value := sUserName(i);
    AdoCmd.Execute;
end;

您可以通过使用.Params(0):Params(1)来加快速度,因为ParamByName需要一些时间。

但这里的诀窍在于ParseSql语句。它使代码保持清晰,但仍然只解析sql字符串一次。

如果需要,您可以使用交易。。。通过使用AdoCmd.Connection.BeginTransAdoCmd\Connection.CommitTrans/RollbackTrans

Proper use of transactions will also speed up your inserts. If each statement needs to be committed, it will take longer to be executed. If you can execute everything within a single transaction and just commit at the end it will be faster. Don t know MySQL, but some databases also support "array DML", where a single SQL statement is sent to the DB together array of parameters and thereby execute multiple times but with a single communication roundtrip.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签