Why can't I insert this into MySQL? - mysql

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

Related

Why is this my sql statement wrong in the navicat? [duplicate]

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());

Subquery with MySql using INSERT

I'm trying to update the database, using a script where the ID of a user isn't readily known, so I'm using a subquery to have mysql find the user id (for the posteruserid value). This is the SQL query i'm using:
INSERT INTO `thread` (`title`, `forumid`, `open`, `replycount`,
`postercount`, `postusername`, `postuserid`, `lastposter`,
`dateline`, `visible`, `keywords`)
SELECT 'IN', 2, 1, 0, 1, 'lemons', `userid` FROM `user`
WHERE `username` = 'lemons', 'lemons', 1375768440, 1, 'IN';
I'm getting a syntax error from the above SQL, and I can't figure out what I'm doing wrong.
EDIT because of the mismatched column name, I tried using an alias, which still doesn't work
INSERT INTO `thread` (`title`, `forumid`, `open`, `replycount`,
`postercount`, `postusername`, `postuserid`, `lastposter`,
`dateline`, `visible`, `keywords`)
SELECT 'IN', 2, 1, 0, 1, 'lemons',
`userid` AS `postuserid` FROM `user` WHERE `username` = 'lemons',
'lemons', 1375768440, 1, 'IN';
column mismatch in insert and select query..column should be same where you are going to insert and from where you are fetching data.
You specify to insert values of 11 columns, but in your SELECT statement, you are providing only 7 values. Please provide the value for lastposter,dateline,visible, and keywords.

Error on SQL insert statement

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

What am I doing wrong with my query syntax?

I am trying to insert a row into my table with the following query in ssms:
INSERT INTO [Reservation].[dbo].[Contacts]
([ContactID]
,[Name]
,[Phone]
,[Email]
,[QoowayUserName])
VALUES
(<"100", nvarchar(50),>
,<"Vincent Chase", nvarchar(50),>
,<"3103331234", nvarchar(50),>
,<"vincent_chase#hollywood.com", nvarchar(50),>
,<"Username", nvarchar(50),>)
GO
I get the error:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near '<'.
I've also tried using single quotes. What am I doing wrong?
INSERT INTO Reservation.dbo.Contacts
(ContactID
,Name
,Phone
,Email
,QoowayUserName)
VALUES
('100'
,'Vincent Chase'
,'3103331234'
,'vincent_chase#hollywood.com'
,'Username')
You might need to remove the "dbo", like this:
INSERT INTO Reservation.Contacts
(ContactID
,Name
,Phone
,Email
,QoowayUserName)
VALUES
('100'
,'Vincent Chase'
,'3103331234'
,'vincent_chase#hollywood.com'
,'Username')

MySql INSERT Failure

Why does the following query fails on MySql?
NHibernate: INSERT INTO UserSetting (userId, key, value) VALUES (?p0, ?p1, ?p2);
?p0 = ccda78da-689d-4d86-ba72-d65eaf281edf [Type: Guid (0)], ?p1 = 'Hello' [Type: String (5)], ?p2 = 'World' [Type: String (5)]
With this 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, value) VALUES ('ccda78da-689d-4d86-ba72-d65eaf281edf', 'Hello', 'World')' at line 1"}
key is a reserved word.
Try this:
INSERT INTO UserSetting (userId, `key`, value) VALUES (?p0, ?p1, ?p2)
Key is a reserved word, use ticks, example in link
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html