mysql syntax error for update rows - mysql

update amazon-crawler set `flag_images`= '0' where `id`='966'
i get #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 '-crawler set flag_images= '0' where id='966'' at line 1
why syntax error?
amazon-crawler is the table
flag images and id are columns

My guess is that the hyphen in the table name is causing problems, because it is an arithmetic operator. Try also escaping the table name:
UPDATE `amazon-crawler` SET `flag_images`= '0' WHERE `id` = '966';
Note that you should try to avoid using backticks in your query, unless absolutely needed. Using backticks means that any name would potentially work, even one which happens to be a MySQL reserved keyword. Also, I'm guessing that flag_images and id are numeric columns, in which case you should be comparing them against numbers, not strings. So I would write your update as this:
UPDATE `amazon-crawler` SET flag_images= 0 WHERE id = 966;
Here, only the table name has to appear in backticks.

Related

Update longtext field in mysql

Im trying to update a longtext type field called 'comment' using a simple sql query in mysql client like this :
Update mytable set comment='Test' where id = 1;
But i'm getting this 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 'comment='Test' where id = 1' at line 1
Am i missing something ?, thanks in advance.
comment is a reserved word, if you want to have a table/field with that name, you have to quote it (or use the table.fieldname syntax, in case of a field). default in mysql is the backtick for that, so:
update mytable set `comment`='Test' where id = 1;
Found it, it gets solved with this:
update mytable as a set a.comment='Test' where id = 1;

query not work for single quote apostrophe

phpmyadmin query not work for single quote / apostrophe.
Not Work
ALTER TABLE 'about_team' CHANGE 'position' 'pp' INT( 11 ) NOT NULL
Work:
ALTER TABLE `about_team` CHANGE `position` `pp` INT( 11 ) NOT NULL
Same query but not work, gives 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 ''about_team' CHANGE 'position' 'pp' INT(11) NOT NULL' at line 1
it is because when you using single quote, it just means it is a STRING. Whereas BACTICK (the second query) means escaping a column.
'about_team' is not equal with `about_team`
'about_team' is STRING
`about_team` is a Table Name
Actually backticks enclosing the names are optional since the names used where not on MySQL Reserved Keyword List.
MySQL Reserved Keywords
Usually, single quotes are used around values while backticks are for table names and column names.

delete query for mysql using c

can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
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 '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).

MySQL Update error while updating table

I have table in MySQL with years as column names like 1960, 1961, 1962... etc. Records are being inserted successfully. When I try to update table with query
UPDATE table1 SET 1960=0.0 WHERE id = 'abc'
it gives:
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 '1960=0.0 WHERE id='abc' at line 1
Is it due to columns names as numbers or something else is wrong?
try this:
UPDATE table1 SET `1960`='0.0' WHERE id = 'abc'
... added backticks to column name and single quotes around value (not really required for the value, but I always do it)
escape your column name with backticks
UPDATE table1 SET `1960` = 0.0 WHERE id = 'abc'
That has to be done if your column name is a reserved keyword in MySQL or a number.
You'll have to escape your column names by using the backtick character. The following manual page is somewhat dense but informative
Try...
UPDATE table1 SET `1960`=0.0 WHERE id = 'abc'

Mysql syntax error during an update but code look fine

I have a strange issue with a Mysql Update and I'm not sure what is causing it. I suspect something is wrong with the table itself but the field causing the error appears to be defined the same as other fields in the table. I can recreate the error in phpMyAdmin on the SQL tab and also in php code. I am completely stumped.
The fields in the table are defined as follows:
bnumber is INT length=11
bname is VARCHAR length=60 Collation=latin1_swedish_ci
twittername is VARCHAR length=15 Collation=latin1_swedish_ci
desc is VARCHAR length=60 Collation=latin1_swedish_ci
This update statement works:
update tbl1 set bname='myName', twittername='myTweet' where bnumber=1;
this one gives me the error:
update tbl1 set bname='myName', twittername='myTweet', desc='test' where bnumber=1;
the error I get is:
#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 'desc='Main' where bnumber=1' at line 1.
I don't seem to have any issues selecting from the table or inserting to the table. Only update is giving me the error.
I appreciate any help.
Thanks!
desc is a reserved word in MySQL. You must quote it in backticks.
desc is a keyword. Escape it with backticks.
update tbl1
set bname='myName',
twittername='myTweet',
`desc`='test'
where bnumber=1;
desc is a reserved word in mysql
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Updated your sql as below and it will work fine;
update tbl1 set bname='myName', twittername='myTweet', `desc`='test'
where bnumber=1;
you cant use desc as a field name.
change fieldname to desc1 and try.
desc is for oder by name desc