Link to home
Start Free TrialLog in
Avatar of moayyad
moayyad

asked on

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.
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

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
Avatar of moayyad
moayyad

ASKER

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.

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
Avatar of moayyad

ASKER

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'');

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
Avatar of moayyad

ASKER

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


Avatar of moayyad

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of moayyad

ASKER

what stupid am i :S

thank u dear its running now, i increaded points to 500 for u .. u deserve it.
Maybe this will make it less confusing,

  query1.SQL.Add('select Address3 from jawwal_table where Address3='#39'zaq'#39);
 
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
btw. parameters could be also used instead