5

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?

3
  • What type of database are you connecting to? Is it MySql, SQL Server, etc? Commented Dec 29, 2011 at 13:31
  • What type is SQLQuery2 - is it a TQuery or a TADOQuery? Commented Dec 29, 2011 at 13:33
  • not Tquery nor Tadoquery it's TSQLQuery. @codeulike Commented Dec 29, 2011 at 13:34

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Remove quotation marks:

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

4 Comments

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
SQL syntax is perfect (I am using Firebird). I can only guess what is wrong in your case, may be you should use a different symbol as a parameter prefix ('?' or '@' instead of ':').
Yep, that corrected the syntax problem, but i still get an error EDatabase Error 'SQLQuery2: parameter 'email' not found'
that is the same error when i use quotations.Thankyou for ure reply !
4

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

5 Comments

your answer is my answer :) glad I could help
You would have saved yourself a day if you've read mjn's answer, and also would have saved the trouble posting an answer yourself.
mjn answer didn't work for me, the problem wasn't only because of the execSQL but also in the query string it self which shouldn't be as i mentioned in my answer but should be entred by using MySQLQuery.SQL.Add . Thankyou for mjn post for help with the execSQL true or false issue.
@Rafik Bari: SQLQuery2.sql.Text := ... and SQLQuery2.sql.Clear; ... SQL.Add both do the same.
Ok! Thankyou anyway your answer also helped me too much!
3

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)

2 Comments

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
That's strange, check if there is unusual whitespace characters? (Retype the query manually, without copy/paste)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.