0

If duplicate, please mark as I cannot otherwise find a solution.

I am executing a very simple SQL command that I have executed many other times in other environments but cannot find a rhyme or reason to this syntax error.

INSERT INTO tracking (range,purchase,trade_id) VALUES ("119.43-119.57","119.50","pid","961971");

I have tried this as well:

INSERT INTO tracking (range,pid,purchase,trade_id) VALUES ('119.43-119.57','119.50','pid','961971');

As well as not quoting numbers (trade_id).

I am receiving error:

ERROR 1064 (42000): 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 'range,purchase,pid,trade_id) VALUES('119.43-119.57','119.50','pid' at line 1

My table structure:

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| trade_id | varchar(125) | NO   | PRI | NULL    |       |
| pid      | varchar(125) | NO   |     | NULL    |       |
| purchase | varchar(125) | NO   |     | NULL    |       |
| range    | varchar(125) | NO   |     | NULL    |       |
| sell     | varchar(5)   | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

And then my SQL version:

+-------------------------+
| VERSION()               |
+-------------------------+
| 5.5.60-0ubuntu0.14.04.1 |
+-------------------------+
1 row in set (0.00 sec)

I am also running on an Ubuntu 14.04 and running these commands from SQL CLI (Command Line Interface).

If anybody has a pointer of something I am blatantly missing, please show the way. I am going batty.

Thank you in advance.

6
  • 1
    Two things: (a) you have 3 fields but 4 values; that’s not going to work (b) Always use single quotes for strings: in MySQL default either is acceptable, but in ANSI mode as well as other databases double quotes would be an error. Commented Jun 11, 2018 at 4:41
  • Thank you! (a) That was an error in traversing from cli to stack, apologies and I will edit my original post to reflect such. (b) Thank you for the clarification on double quotes versus single quotes. Commented Jun 11, 2018 at 4:45
  • 1
    range is a keyword and must be escaped. Or bettet rename the column Commented Jun 11, 2018 at 4:45
  • That actually maybe it, I should rename the column. Will let you know if that resolves. At which point I will accept answer if issue resolved. Commented Jun 11, 2018 at 4:46
  • Would you mind answering the question and I will accept? That resolved the issue. Commented Jun 11, 2018 at 4:52

4 Answers 4

1

Documentation

Certain keywords, such as SELECT, DELETE, or BIGINT, are reserved and require special treatment for use as identifiers such as table and column names

The simplest solution is simply to avoid using reserved words as identifiers.

Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, "range" is a reserved term. Altering column name resolved the issue. Thanks a ton.
1

You can try this

INSERT INTO tracking (`range`,`pid`,`purchase`,`trade_id`) VALUES (`119.43-119.57`,`119.50`,`pid`,`961971`);

3 Comments

values can not be quoted with backticks. Ist must be quoted with single quotes
"range" is a reserved term per documentation and would need to be escaped or avoided. See Jens comment and deadman's (accepted) solution.
I know range is a reserved key word,but in some case we accidentally use it and that becomes part of POC.Sounds bad but it's true.So backticks is there to help you as written in deadman's (accepted) solution.
1

Have you tried this out

INSERT INTO 'tracking' (range,pid,purchase,trade_id) VALUES ('119.43-119.57','119.50','pid','961971');

4 Comments

Do not use single quotes around table names
@Jens can you please let me know why we have not to use quotes.
Because single quotes encloses strings, not column or table names
-1

Fixed query by suggestion from deadman:

INSERT INTO `tracking` (`range`, `purchase`, `pid`, `trade_id`) VALUES ("119.43-119.57", "119.50", "pid", "961971");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.