Syntax Error in Insert to statement for Delphi 10.2 Tokyo

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Syntax Error in Insert to statement for Delphi 10.2 Tokyo



I need help with my coding, I have tried changing variables and displaying the SQL and I still can not see the error



Thank you.



Here is my code:


qryCustomerInformation.SQL.Add('/*The SQL query string from below*/');
showmessage(qryCustomerInformation.SQL.Text);
qryCustomerInformation.ExecSQL;



Here is my query string:


INSERT INTO BasicInformation (
Passwords,
CustomerID,
Names,
Surname,
Age,
IdentitiyNumber,
Address,
CardNumber,
CSV,
Expirey,
Image,
PhoneNumber
) VALUES (
''' + CustomerPassword +
''',''' + CustomerId +
''',''' + CustomerName +
''',''' + CustomerSurname +
''',''' + CustomerAge +
''',''' + CustomerIdNumber +
''',''' + CustomerAddress +
''',''' + CustomerCardNumber +
''',''' + CustomerCSV +
''',''' + CustomerExpirey +
''',''' + CustomerImage +
''',''' + CurstomerPhoneNumber
+ ''')





A screenshot like the one you have provided is absolutely no use for getting help here. Please copy/paste the text of your Sql into into your question. Meanwhile, -1
– MartynA
Aug 10 at 8:24





If any of the variables have a single quotation mark in them, you're going to retrieve a syntax error if you have not escaped the variables and it will break your query string. Due to the fact that you've provided very little information on this, it is hard to tell what your issue is.
– Matt
Aug 10 at 8:28





Gah. Use parameters. Avoid quoting issues and defend from SQL injection.
– David Heffernan
Aug 10 at 9:03





Using parameters also makes dealing with date/time types much easier. Be lazy, make your own life easier, use params. That's what they are there for.
– Gerry Coll
Aug 12 at 12:01




1 Answer
1



Part of the reason that you are having problems is that your SQL is syntactically
unnecessarily complicated. Assuming CustomerPassword, etc are all string variables,
you can simplify your Sql as follows:


CustomerPassword


var InsertSql : String;

begin
[...]
InsertSql :=
'INSERT INTO BasicInformation ('
+ ' Passwords, '
+ ' CustomerID, '
+ ' Names, '
+ ' Surname, '
+ ' Age, '
+ ' IdentitiyNumber, '
+ ' Address, '
+ ' CardNumber, '
+ ' CSV, '
+ ' Expirey, '
+ ' Image, '
+ ' PhoneNumber'
+ ')'
+ ' VALUES ('
+ QuotedStr(CustomerPassword) + ', '
[... etc]
+ QuotedStr(CurstomerPhoneNumber) + ')'
;

qryCustomerInformation.SQL.Add(InsertSql);



I have added the InsertSql local variable to avoid constructing the Sql within
the call to qryCustomerInformation.SQL.Add(), because that frequently results in errors itself.



The code above uses QuotedStr around the values of the string variables. It not only supplies the necessaryu surrounding quotes but also correctl handles the case where the variable's value includes embedded single-quote marks.


QuotedStr



However, as David Heffernan says, it is better to use a parameterised query,
for which you need to add a parameter to qryCustomerInformation for each value
in the Values clause, and modify the InsertSql so that it refers to each of them
instead of the original string value, like so


Values


+ ' VALUES ('
+ ':CustomerPassword, '
etc.



qryCustomerInformation will automatically substitute the parameter values for the labels prceded by colons.


qryCustomerInformation



Btw, your code contains a number of spellings which look suspicious to me:
Expirey
CustomerExpirey
CurstomerPhoneNumber





Martyn Although I also strongly agree that he should switch to parameters to prevent SQL injections, in your first solution you are forgetting to quote the values. You should replace CustomerPassword by QuotedStr(CustomerPassword), and repeat that for every other value.
– Marc Guillot
Aug 10 at 10:34






Indeed. Thanks very much for spotting @MarcGuillot!
– MartynA
Aug 10 at 11:14







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard