Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

INSERT INTO troubles

 
Joel Christophel
Ranch Hand
Posts: 249
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code doesn't seem to insert a row into the table 'students' as I'd like it to.
The SQLException getMessage() method returns the following:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, Last Name, School ID Number) VALUES('Joel', 'Christophel', '101059')' at line 1
 
Paul Clapham
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you really give your columns names which have spaces in them, like "First Name"? If so, then you're going to have to escape them to make the SQL which contains them into valid SQL.

I think that might require putting quotes around them, but I don't see anything about that in the MySQL documentation except for something which says that you escape reserved words by putting a back-tick before them.
 
Joel Christophel
Ranch Hand
Posts: 249
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Escaping quotation marks around the column names didn't work, so I just removed the spaces from the column names, which allowed me to insert the row. Thanks for your help!

EDIT: After further research, I found the using back ticks (as you mentioned) works for column names with spaces. http://stackoverflow.com/questions/6055505/blank-spaces-in-column-names-with-mysql
 
Paul Clapham
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback Joel!

(In my opinion, using standard column names -- no spaces -- is preferable to having to escape them with back-ticks every time you use them in a query.)
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Thanks for the feedback Joel!

(In my opinion, using standard column names -- no spaces -- is preferable to having to escape them with back-ticks every time you use them in a query.)


I seriously dislike have spaces in column names, but they all seem to allow it these days.
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:(In my opinion, using standard column names -- no spaces -- is preferable to having to escape them with back-ticks every time you use them in a query.)

+1 for that. Putting spaces in your table/column names, or making them case-sensitive, just gives you another potential source of errors further down the line.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic