SQL statement error

Dear all,

i'm new to dephi and programming and wanna write this query, but i had error while running the program.

i wanna select all records from mytable where the name equal the string "chad", i think the problem in writing string.
the code is as follows:  
query1.SQL.Add('select * from mytable where name=chad');

i wish u help me to find the correct statment.
moayyadAsked:
Who is Participating?
 
Russell LibbySoftware Engineer, Advisory Commented:
I will try one last time.... please read carefully. Its single quotes, 2 before and 3 at the end. It is NOT double quotes. Delphi uses ' to start and end a string. If you need to embed the ' char in a string then it needs to be doubled up.

''  // tick tick

which is not

" // Double quotes

0
 
Russell LibbySoftware Engineer, Advisory Commented:
You need to close the string with single quotes (sometimes double quotes work depending on the driver, but single quotes should always work)

Eg:

 query1.SQL.Add('select * from mytable where name=''chad''');

Regards,
Russell
0
 
moayyadAuthor Commented:
thaks for your repy,
but with single qoutes i have an error with the statement it self. and also with double quotes the program runs but when executing the query it gives an error message:
General SQL Error
[Microsoft][ODBC Microsoft Access Driver] too fiew parameters. expected 1.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Russell LibbySoftware Engineer, Advisory Commented:

What is the error message when using the single quotes? (exact message would be helpful). Also, double check that the following executes:

'select * from mytable'

If this does not work, then you have other problems. If it does work, then I'm not sure what to tell you (other than to double check the column name in the database). Just to check this, I set up an access MDB, added a table called 'mytable' with a TEXT column called 'name' and I can exec the sql statement against it without problems.

Russell
0
 
moayyadAuthor Commented:
yes dear,
i tried to run the same query without (where conidtion) and it executed without problems.


as u reqested i have this errors:
Build
  [Error] jawwal.pas(40): ')' expected but identifier 'zaq' found
  [Fatal Error] jawwall.dpr(5): Could not compile used unit 'jawwal.pas'

and the compiler points to this statment of code:
query1.SQL.Add('select Address3 from jawwal_table where Address3='zaq'');
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Thats better...
You have 2 typos in your code (please recheck what I gave you). It needs to start with 2, not 1 tick, and needs to end with 3, not 2 ticks:

  query1.SQL.Add('select Address3 from jawwal_table where Address3=''zaq''');

As a suggestion, you should spend some time going over the Delphi help regarding Character strings.

Russell
0
 
moayyadAuthor Commented:
mmm i'm sorry but i don't understand u :(

"You have 2 typos in your code (please recheck what I gave you). It needs to start with 2, not 1 tick, and needs to end with 3, not 2 ticks: "

what do u mean please?
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Compare the following, they are not the same:

query1.SQL.Add('select Address3 from jawwal_table where Address3='zaq''); <- WRONG

query1.SQL.Add('select Address3 from jawwal_table where Address3=''zaq'''); <- RIGHT


0
 
moayyadAuthor Commented:
oh, ok
i told u dear, this gives an error while executing the query..

this this code:
http://smttx.jeeran.com/error.JPG


this the error message:
http://smttx.jeeran.com/error2.JPG
0
 
moayyadAuthor Commented:
what stupid am i :S

thank u dear its running now, i increaded points to 500 for u .. u deserve it.
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Maybe this will make it less confusing,

  query1.SQL.Add('select Address3 from jawwal_table where Address3='#39'zaq'#39);
 
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Lol, its ok. I realized after the multiple posts that you were confusing the 2 single quotes with a double quote, and being new to delphi, it probably wasn't making sense.

Regards,
Russell
0
 
kretzschmarCommented:
btw. parameters could be also used instead
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.