PGSQL- syntax error at or near “-“

Posted on

Question :

I have tried to run an INSERT query in Pgsql using PgAdmin. The table name is todo-list. However, when I try to run it, an error message appears:-

syntax error at or near "-"

LINE 1: INSERT INTO todo-list(todo_title,todo_description,todo_image...

The query that I want to run is

INSERT INTO todo-list(todo_title,todo_description,todo_image,todo_status) VALUES("efrgthnj","wefrgtbhnj","qwefgrthj",1)

I tried using “todo-list”, but that didn’t work either. What should I do?

Answer :

As others have quite rightly pointed out, your proposed table name (todo-list) does not conform to the MySQL rules for an Identifier and so MySQL can’t work out what to do with it.

You could try wrapping it in back-ticks (`, over by the “1” key on your keyboard) – not single (‘) or double (“) quotes – as in …

insert into `todo-list` ( ... 

… but, frankly, you’ll be far better off not getting into the habit of using “invalid” identifiers in the first place. Eventually, they always come back and bite you.

All lower case names with underscores in between “words” is a perfectly good convention, as you appear to have used for all your column names.

Leave a Reply

Your email address will not be published. Required fields are marked *