[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL statement error

Posted on 2006-05-20
13
Medium Priority
?
210 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:moayyad
  • 7
  • 5
13 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16724300
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
 

Author Comment

by:moayyad
ID: 16724316
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16724380

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:moayyad
ID: 16724406
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16724431

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
 

Author Comment

by:moayyad
ID: 16724440
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16724452
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
 

Author Comment

by:moayyad
ID: 16724463
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
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 16724513
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
 

Author Comment

by:moayyad
ID: 16724529
what stupid am i :S

thank u dear its running now, i increaded points to 500 for u .. u deserve it.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16724531
Maybe this will make it less confusing,

  query1.SQL.Add('select Address3 from jawwal_table where Address3='#39'zaq'#39);
 
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16724538
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 16740596
btw. parameters could be also used instead
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question