We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,617 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Where is the problem in this SQL statement?

Hi,

I recieve the following error message:
"java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression'name '"

when I run this statement:

sql = "INSERT INTO empDetail VALUES(";
        sql += txtName.getText()+ ", ";
        sql += "'" + txtAddress.getText()+ "', ";
        sql += "'" + txtSuburb.getText()+ "', ";
        sql += "'" + txtPostCode.getText()+ ", ";
        sql += "'" + txtDOB.getText()+ "', ";
        sql += "'" + txtPhone.getText() + "', ";
        sql += "'" + txtWorkExt.getText()+ "', ";
        sql += "'" + txtMobile.getText()+ "', ";
        sql += "'" + txtEmail.getText()+"')";

I can't see the problem where is the missing operator?(and yes I have my glasses on :)
I have been going over and over it the last 2hrs but without any luck.
can you spot what's going on.

3
Contributors
3
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
Sonny101
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You're missing the ' before the comma in this line:

sql += "'" + txtPostCode.getText()+ ", ";

Sometimes it just needs a new pair of eyes:)

hericles
Veteran Poster
1,065 posts since Nov 2007
Reputation Points: 156
Solved Threads: 228
Skill Endorsements: 10

You're missing the ' before the comma in this line:

sql += "'" + txtPostCode.getText()+ ", ";

Sometimes it just needs a new pair of eyes:)

Thank you,
I have changed that but I am still receiving the same error

Sonny101
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

There could be any number of problems, but the most likely is that you have not listed the column names that you expect to populate with the data listed in the VALUES clause. Generally speaking, it is good procedure to explicitly list the column names like so (and these names are fictitious...just for example's sake):

sql = "INSERT INTO empDetail ";
sql += "(empName, empAddress, empSuburb, empPostCode, empDOB, empPhone, empWorkExt, empMobile, empEMail) ";
sql += "VALUES('" + txtName.getText() + "', ";
sql += "'" + txtAddress.getText() + "', ";
sql += "'" + txtSuburb.getText() + "', ";
sql += "'" + txtPostCode.getText() + ", ";
sql += "'" + txtDOB.getText() + "', ";
sql += "'" + txtPhone.getText() + "', ";
sql += "'" + txtWorkExt.getText() + "', ";
sql += "'" + txtMobile.getText() + "', ";
sql += "'" + txtEmail.getText() + "')";

Let's say for instance that the table has an identity column "empID". If you don't explicitly list the column names, the INSERT statement will assume you mean for your first value to go into that column.

There could also be mismatched datatypes, but we'll ignore that for now. Or, heaven forbid, you get your columns in a different order than your VALUEs.

As a final word of advice, whenever you get odd errors you may want to just print the value of your variable "sql" and paste in an SSMS query window and see if it actually works. Sometimes when you strip away all the dynamic construction code and see what's actually being executed, the error will jump out at you.

Hope this helps! Good luck!

BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14
Question Answered as of 1 Year Ago by hericles and BitBlt

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0706 seconds using 2.69MB