How use the insert query using parameters?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 22:50:31

From the TSQLQuery.ExecSQL documentation:

ExecDirect indicates that the query does not need to be prepared before it is executed. This parameter can be set to true if the query does not include any parameters.

So if the code uses

SQLQuery2.ExecSQL(true);

this means that there will be no support for parameters.

But because you use parameters, just use

SQLQuery2.ExecSQL;

and also remove the quotes around parameters.

Remove quotation marks:

SQLQuery2.sql.Text := 'INSERT INTO registered (email,login_pass,payment_method,operateur)
   VALUES (:email, :login_pass, :payment_method, :avecpuce)';

You don't usually quote parameters, only literals. So instead of:

VALUES (":email",":login_pass",":payment_method",":avecpuce")

Try:

VALUES (:email,:login_pass,:payment_method,:avecpuce)

Found the answer !

MySQLQuery2.SQL.Clear;
MySQLQuery2.SQL.Add('INSERT INTO COUNTRY (NAME, CAPITAL, POPULATION)');
MySQLQuery2.SQL.Add('VALUES (:Name, :Capital, :Population)');
MySQLQuery2.Params[0].AsString := 'Lichtenstein';
MySQLQuery2.Params[1].AsString := 'Vaduz';
MySQLQuery2.Params[2].AsInteger := 420000;
MySQLQuery2.ExecSQL;

Thankyou All !!

You should not use quotes around the parameter name.

Parameters are automatically generated for you if your TSQLQuery has a connection assigned and ParamCheck is true and you assign TSQLQuery.CommandText.

It will not generate the parameters when you assign the query to TSQLQuery.SQL.Text.

You can have the parameters generated for you by calling TSQLQuery.Params.ParseSQL:

SQLQuery2.Params.ParseSQL(SQLQuery2.SQL.Text, True);

Or you can add them yourself by calling TSQLQuery.Params.AddParameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!