• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

INSERT INTO troubles

 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic