This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
INSERT INTO ry_useraccount
(
'Id',
'UserId',
'FreeAmount',
'PayFrozenAmount',
'WithdrawFrozenAmount',
'CurrentAsset',
'CreateTime',
'UpdateTime'
) VALUES
(
'1',
'52501',
'600',
'0',
'0',
'0',
NOW(),
NOW()
);
If just look at the sql,it is correct,but when I run it,the navicat said
“[SQL]INSERT INTO ry_useraccount('Id', 'UserId', 'FreeAmount',
'PayFrozenAmount', 'WithdrawFrozenAmount', 'CurrentAsset',
'CreateTime', 'UpdateTime') VALUES('1', '52501', '600', '0', '0', '0',
NOW(), NOW()); [Err] 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 ''Id', 'UserId', 'FreeAmount',
'PayFrozenAmount', 'WithdrawFrozenAmount', 'Curren' at line 1”
. I cannot find the reason.I guess that maybe some white space cause the result,but after I delete all the white spaces,it still didn't work.
The single quote are for literal string not for column name (you can use backticks if you need column name with space or with reserved words )
INSERT INTO ry_useraccount(Id, UserId, FreeAmount, PayFrozenAmount, WithdrawFrozenAmount, CurrentAsset, CreateTime, UpdateTime)
VALUES(1, 52501, 600, 0, 0, 0, NOW(), NOW());
Related
Update: This issue was resolved in the comments and is awaiting an answer
When executing the following query in PyMySQL, I receive the error ‘1064, u"You have an error in your SQL syntax;’ (full error message below)
INSERT INTO `table_name` (`id`, `colName1`, `colName2`, `colName3`)
VALUES (820, 'string', 5, 'N')
ON DUPLICATE KEY UPDATE
`colName1`=VALUES(`colName1`),
`colName2`=VALUES(`colName2`),
`colName3`=VALUES(`colName3`)
-- Tried with and without ` surrounding column names after ON DUPLICATE KEY UPDATE
Syntax in Python:
sql = "INSERT INTO `table_name` (`id`, `colName1`, `colName2`, `colName3`) VALUES (820, 'string', 5, 'N') ON DUPLICATE KEY UPDATE `colName1`=VALUES(`colName1`),`colName2`=VALUES(`colName2`),`colName3`=VALUES(`colName3`)"
I received this error both when trying to update many rows (as suggested elsewhere, using the format of the accepted answer), and for one row at a time.
As noted in the comments, the stripped down example didn't produce an error (although it was not executed with Python). The following is the actual query. Let me know if other information is needed.
Query string executed in Python:
INSERT INTO `Listings` (`id`, `row_last_updated`, `maxBidCountPreRsvMet`, `maxBidPreRsvMet`, `minBidCountPostRsvMet`, `occupancy_status`, `waitingForHnb`, `ownItNow_price`, `high_bid`, `prop_id`, `hot_property`, `status`, `end_date`, `reserve_met`, `hours`, `backupBidSet`, `listing_type`, `low_bid`, `winning_bid`, `bids`, `days`, `high_bid_updated`, `minutes`, `lowBidIsOpt1`) VALUES (820, '2018-01-28 19:16:02', '5', '234000', None, 'N', None, 0, 234000, u'9007092665103', 'N', 'New', '2018-1-31-22-0', 'N', 7, 0, 'AUCN', 0, 0, 5, 3, '2018-01-28 14:16:02.001906', 44, 0) ON DUPLICATE KEY UPDATE `row_last_updated`=VALUES(`row_last_updated`),`maxBidCountPreRsvMet`=VALUES(`maxBidCountPreRsvMet`),`maxBidPreRsvMet`=VALUES(`maxBidPreRsvMet`),`minBidCountPostRsvMet`=VALUES(`minBidCountPostRsvMet`),`occupancy_status`=VALUES(`occupancy_status`),`waitingForHnb`=VALUES(`waitingForHnb`),`ownItNow_price`=VALUES(`ownItNow_price`),`high_bid`=VALUES(`high_bid`),`prop_id`=VALUES(`prop_id`),`hot_property`=VALUES(`hot_property`),`status`=VALUES(`status`),`end_date`=VALUES(`end_date`),`reserve_met`=VALUES(`reserve_met`),`hours`=VALUES(`hours`),`backupBidSet`=VALUES(`backupBidSet`),`listing_type`=VALUES(`listing_type`),`low_bid`=VALUES(`low_bid`),`winning_bid`=VALUES(`winning_bid`),`bids`=VALUES(`bids`),`days`=VALUES(`days`),`high_bid_updated`=VALUES(`high_bid_updated`),`minutes`=VALUES(`minutes`),`lowBidIsOpt1`=VALUES(`lowBidIsOpt1`)
The full error message:
(1064, u"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 ''9007092665103', 'N', 'New', '2018-1-31-22-0', 'N', 7, 0, 'AUCN', 0, 0, 5, 3, '2' at line 1")
Side notes: Python None values are converted to MySQL null values. The same error occurred when changing None values to strings.
I'm using import to add rows to a table. I have almost 600 to add but working with just the 2 below for now. My file:
INSERT INTO `wp_wlm_userlevel_options` ( `userlevel_id`, `option_name`, `option_value`, `autoload`) VALUES ( '341', registration_date', '2017-11-30 16:14:43#0', 'yes' ),( '341', 'transaction_id', 'WL-6806-1506616728', 'yes' );
There is an auto generated value (ID) before userlevel_id. I've tried including it with same results. Also tried removing "16:14:43#0" same results.
Full response***********************
Error
Static analysis:
2 errors were found during analysis.
Unexpected character. (near ":" at position 153)
Unexpected character. (near ":" at position 156)
SQL query:
INSERT INTO `wp_wlm_userlevel_options` ( `userlevel_id`, `option_name`, `option_value`, `autoload`) VALUES ( '341', registration_date', '2017-11-30 16:14:43#0', 'yes' ), ( '341', 'transaction_id', 'WL-6806-1506616728', 'yes' )
MySQL said: Documentation
#
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 '', '2017-11-30 16:14:43#0', 'yes' ), ( '341',
'transaction_id', 'WL-6806-1506' at line 2)
Any help appreciated!! Thanks
I want to tab space my data while inserting through sql in phpmyADMIN.
What should I do?
INSERT INTO `questions` (`id`, `question_name`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer`, `category_id`) VALUES(90,'C<br> java', '1', '2', '3', '4', '', '', '3', 2);
You can split the string up and place a tab in between: 'some' + CHAR(9) + 'string'. Note that 9 is the ASCII code for a horizontal tab. Perhaps easier is 'some\tstring', which is syntax common to many other languages.
See table 9.1 here: http://dev.mysql.com/doc/refman/5.0/en/string-literals.html
I exported a recordset from one database into a csv file, and when I try to import it into another using mysql workbench I keep this this error message:
Executing SQL script in server
ERROR: 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 ' 'Lord it Over', 'Ben', '1993-03-01', 'TRC', NULL, 1983, '1999-09-01', 'NULL', '' at line 1
INSERT INTO `TRC`.`horse`
(`horse_id`, `registered_name`, `stable_name`, `arrival_date`, `last_known_location`, `is_ex_racer`, `birth_year`, `death_date`, `horse_comments`, `sex`, `referral_date`, `horse_height`, `arrival_weight`, `passport_no`, `microchip_no`, `is_on_waiting_list`) VALUES
(, 'Lord it Over', 'Ben', '1993-03-01', 'TRC', NULL, 1983, '1999-09-01', 'NULL', 'NULL', 'NULL', NULL, NULL, 'NULL', 'NULL', 0)
SQL script execution finished: statements: 29 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Any help would be appreciated as their appears to be no errors as far as I can see.
It's becaose of
VALUES starts with a comma.
You have missed horse_id. If it's Identity Column Then Remove horse_Id
Try like this (If horse_Id is identity)
INSERT INTO `TRC`.`horse`
(`registered_name`, `stable_name`, `arrival_date`, `last_known_location`, `is_ex_racer`, `birth_year`, `death_date`, `horse_comments`, `sex`, `referral_date`, `horse_height`, `arrival_weight`, `passport_no`, `microchip_no`, `is_on_waiting_list`) VALUES
('Lord it Over', 'Ben', '1993-03-01', 'TRC', NULL, 1983, '1999-09-01', 'NULL', 'NULL', 'NULL', NULL, NULL, 'NULL', 'NULL', 0)
Or (If Horse_id simple int then try this)
INSERT INTO `TRC`.`horse`
(`horse_id`, `registered_name`, `stable_name`, `arrival_date`, `last_known_location`, `is_ex_racer`, `birth_year`, `death_date`, `horse_comments`, `sex`, `referral_date`, `horse_height`, `arrival_weight`, `passport_no`, `microchip_no`, `is_on_waiting_list`) VALUES
('1','Lord it Over', 'Ben', '1993-03-01', 'TRC', NULL, 1983, '1999-09-01', 'NULL', 'NULL', 'NULL', NULL, NULL, 'NULL', 'NULL', 0)
^^^^ -- Here Horse_Id missing
it seems that you missing your "horse_id" value.
If that field was intended to be the Identity field, then you shouldn't mention that field on Insert Statement right?
If you want MySQL to generate an auto_increment ID for the horse_id field, you should either leave it out of the INSERT statement entirely, or specify NULL for the value in the VALUES list. You can't just leave that value empty in the list.
Your VALUES starts with a comma for some reason
I probably made a stupid error on my syntax for the query, but I can't seam to fix it. Here is the query my program tries to execute:
INSERT INTO filez (
filename,
uploadedby,
dateuploaded,
public,
FileSize,
FileTransferSize,
key,
bytes
)
VALUES(
'tacct/tesABCscdsdasdasdD.testtest',
'tacct',
'%27 %December %2012, %7:%32:%15%AM',
1,
7,
7,
'`',
'TestDoc'
)
And here's the mysql_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 'key, bytes) VALUES('tacct/tesABCscdsdasdasdD.testtest', 'tacct', '%27 %D' at line 1
I did mysql_real_escape_string() on EVERYTHING except the FileSize and FileTransferSize in the query. Could you tell me what I am doing wrong? Thanks!
The error tells you that the error happens at the text key, and it's quite correct: key is a reserved word in MySQL.
Where you use it as a field name, you must enclose it in backticks (`); it's a good general practice anyway.
So:
INSERT INTO `filez` (
`filename`,
`uploadedby`,
`dateuploaded`,
`public`,
`FileSize`,
`FileTransferSize`,
`key`,
`bytes`
)
VALUES(
'tommy3244/tesABCscdsdasdasdD.testtest',
'tommy3244',
'%27 %December %2012, %7:%32:%15%AM',
1,
7,
7,
'`',
'TestDoc'
)
key is a reserved word and you can not use it in column name without using backtics. try this
INSERT INTO filez (`filename`, `uploadedby`, `dateuploaded`, `public`, `FileSize`, `FileTransferSize`, `key`, `bytes`) VALUES('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244', '%27 %December %2012, %7:%32:%15%AM', 1, 7, 7, '`', 'TestDoc')
try using following query
INSERT INTO filez (filename, uploadedby, dateuploaded, public, FileSize, FileTransferSize, `key`, bytes) VALUES
('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244', '%27 %December %2012, %7:%32:%15%AM',
1, 7, 7, '`', 'TestDoc')
key is keyword so you have to use backticks as i used above.
For more info check following question too
Select a column with a keyword name
INSERT INTO `filez` (`filename`, `uploadedby`, `dateuploaded`,
`public`, `FileSize`, `FileTransferSize`,
`key`, `bytes`)
VALUES('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244',
'%27 %December %2012, %7:%32:%15%AM', 1, 7, 7, '`', 'TestDoc')
ok,
syntax to use near 'key, bytes) both are reserve words, Good practice is always use ` sign for user defined identifiers.
KEY is reserved key word for mysql
try other column name or make backticks like that ``
The issue is key is the reserved keyword of mysql , you can't use it as column name, if you really want then enclose it inside backtick character
key