Rafik Bari
Rafik Bari

Reputation: 5037

How use the insert query using parameters?

When i try with this query i get an error says that Perameter email doesn't exist, i am sure that the variables : email, login_pass, payment_method,operateur are valid and exists.

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

I tried removing the quotation, but i get

You have an error in your Sql syntax, check the manual that corresponds to your SQL server for the right syntax to use near ':email,:login_pass,:payment_method,:avecpuce)' at line 1

How to use the insert query above using parameters?

Upvotes: 5

Views: 33970

Answers (5)

Rafik Bari
Rafik Bari

Reputation: 5037

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 !!

Upvotes: 4

mjn
mjn

Reputation: 36644

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.

Upvotes: 7

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

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.

Upvotes: 1

Andomar
Andomar

Reputation: 238078

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)

Upvotes: 3

kludg
kludg

Reputation: 27493

Remove quotation marks:

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

Upvotes: 5

Related Questions