Error in SQL Update Syntax [duplicate] - mysql

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I am trying to update a database via an update query,but I am getting an error that says
Error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'add=null,col=null,wok=null,pcol=null,pwok=null,bio=null where un ='null'' at line 1.
I have been trying for hours to figure out what the error is in my SQL Syntax,but I haven't been able to get through.
The update query is:
query = "Update users SET em=?, mn=?,add=?,col=?,wok=?,pcol=?,pwok=?,bio=? where un ='"+un+"'";
I need help in figuring out the error in my update syntax,Thanks.

ADD is a reserved word in mysql use backticks
\`add\`=?
http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html

ADD is reserved word. Please quote it or change field name

From Reserved Words
the word add is a reserved keyword
Reserved words are permitted as identifiers if you quote them as
described in Section 9.2, “Schema Object Names”:
From Schema Object Names
An identifier may be quoted or unquoted. If an identifier contains
special characters or is a reserved word, you must quote it whenever
you refer to it.

Related

Can Doctrine save fields which are reserved keys?

I have a table with "from", "with" columns too. When I want to persist it, I get an exception:
PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'from, with, fb_email, fb_id, fb_login_code, fb_hometown, fb_location, fb_tagged_' at line 1' in
I affraid the "from" and "with" names causes this, any idea?
As documented under Quoting Reserved Words:
Sometimes it is necessary to quote a column or table name because of reserved word conflicts. Doctrine does not quote identifiers automatically, because it leads to more problems than it would solve. Quoting tables and column names needs to be done explicitly using ticks in the definition.
<?php
/** #Column(name="`number`", type="integer") */
private $number;
Doctrine will then quote this column name in all SQL statements according to the used database platform.
Warning
Identifier Quoting does not work for join column names or discriminator column names unless you are using a custom QuoteStrategy.
For more control over column quoting the Doctrine\ORM\Mapping\QuoteStrategy interface was introduced in 2.3. It is invoked for every column, table, alias and other SQL names. You can implement the QuoteStrategy and set it by calling Doctrine\ORM\Configuration#setQuoteStrategy().
The ANSI Quote Strategy was added, which assumes quoting is not necessary for any SQL name. You can use it with the following code:
<?php
use Doctrine\ORM\Mapping\AnsiQuoteStrategy;
$configuration->setQuoteStrategy(new AnsiQuoteStrategy());
For yaml orm mapping you have to specify column with escaped quotes :
fields:
order:
column: "`order`"
type: integer

Syntax error in update when using where clause

I'm stuck in an update query. I'm working on registration form where if confirm mail link is been redirected to site then update query pass and update row with confirm value.
Here is the error message:
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ''users' SET 'confirm'='1' WHERE
'com_code'='732aabcb4ad6a03b51e0a55aab998726'' at line 1
Please check where my syntax is wrong:
UPDATE 'users'
SET 'confirm'='1'
WHERE 'com_code'='732aabcb4ad6a03b51e0a55aab998726';
Thanks!
To quote identifiers use backticks ` Identifier Names
Identifiers may be quoted using the backtick character - `. Quoting is
optional for identifiers that don't contain special characters, or is
a reserved word. If the ANSI_QUOTES SQL_MODE flag is set, double
quotes (") can also be used to quote identifiers.
UPDATE `users`
SET `confirm`='1'
WHERE `com_code`='732aabcb4ad6a03b51e0a55aab998726';
or don't use them at all if your identifiers aren't keywords or don't contains spaces and so on:
UPDATE users
SET confirm ='1'
WHERE com_code='732aabcb4ad6a03b51e0a55aab998726';
You don't need to put confirm, users and com_code inside quotes,
use this:
UPDATE users
SET confirm ='1'
WHERE com_code='732aabcb4ad6a03b51e0a55aab998726';

when i will run this query it will give an error

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 'specific) VALUES ('5.jpg','kids','anyone')' at line 1,,
My query is
$sql="INSERT INTO imagetable(image,name,specific) VALUES ('$dbimage','$dbname','dbspec')";
specific is a reserved word in MySQL. Either use backticks to escape it or use another name for your column.
INSERT INTO imagetable (image, name, `specific`)
VALUES ('$dbimage','$dbname','dbspec')
sql="INSERT INTO `imagetable` (`image`,`name`,`specific`) VALUES ('$dbimage','$dbname','dbspec')";
While not required, it is a good practice to surround your column names (and table names) with ` characters. This avoids issues with reserved words used by the SQL language.
The reason you are getting this issue is because "specific" is a reserved keyword by the SQL language. Think of it like trying to name a variable "if". Since the keyword "if" is reserved by the coding language, you cannot do this. It is the same concept with "specific" in SQL.

How to access table when the name is a keyword [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I just realized that i assigned a table name as "AS" and when i was trying to do a select query, i kept getting an 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 '"as"'.
So i looked up for reserved words and found out that "AS" is reserved. Well, i think i knew before (used for aliases) but just didn't consider it.
So to fix this will easily be to rename the table name. But assuming i don't want to, can i still access this table using some sort of symbol ? i tried putting it in quotes and double quotes but no success.
With mysql, you can wrap reserved words (or any words for that matter) in backticks to cause word to be parsed as a literal name rather than the keyword:
select * from `AS`
Read more about it in the on line documentation under "identifier quote character".
Yes use this(backtiks):
SELECT * FROM `as`
You should use backticks. Else MySQL will consider it as a Keyword. If we use backtick then they are called quoted identifiers and they tell the parser to handle the text between them as a literal string. They are useful for when you have a column or table that contains a keyword or space.
Please refer: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

MYSQL - Set/Update Syntax [duplicate]

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I have a table like this:
Why is this command not working:
UPDATE 'stitch' SET 'claim-time'='20' WHERE 'group'='010000'
I get the error:
#1064 - 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 ''stitch' SET claim-time='20' WHERE group='010000'' at line 1
Everything in the table is text.
group is a reserved keyword in mysql so use backticks to escape it
`group`
Also you are selecting the string as column name, correct format is
UPDATE `stitch` SET `claim-time`='20' WHERE `group`='010000'
Try removing the single quotes from stitch, claim-time and group. Either leave them out or use backquote `. The comma is used for strings, not table and field names.
Also, I don't know what data type claim-time and group are. If they are numeric (int, bigint, etc) and not string (varchar, text, etc) then you'll need to remove the single quotes from those too.
update stitch set claim-time=20 where group='0100000'; # assuming group is a string data type
Try this.
UPDATE TableName SET claim-time='20' WHERE group='010000';
That is considering if claim-time is a varchar datatype. If it's a number just remove the quotes.
Remember to avoid reserved names such as field names such as name, password, group, user and stuff just to be safe. Make it user1, group1 instead or something like these.