My code keeps giving me the following 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 'INSERT INTO tags (id, name) VALUES (1, 'Web Design')' at
line 2
I'm running the code on phpMyAdmin. Here is the code:
USE home_blog
INSERT INTO tags (`id`, `name`)
VALUES (1, 'Web Design');
id is an int and name is a varchar. I found similar problems, but they were all fixed by reserved word issues or missing parentheses. Unless I'm completely blind I don't those issues.
USE and INSERT are 2 different statements, like 2 queries. You need to write ";" at the very end of all statements if you want to execute them in the row.
Related
I am using node to connect to mysql and I need to run an insert and then immediately run a select last_insert_id().
insert into data_temp values (null, '{"test":{"id":12,"otherdata":"x","otherdata2":"y"}}');
SELECT LAST_INSERT_ID();
In mysql workbench this query works but gives me an error saying:
SELECT LAST_INSERT_ID() )
Error Code: 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 ')' at line 2 0.047 sec
It actually returns the correct id but because it is throwing an error on the last part node is catching the error.
There is an issue in your insert query you just missed to put the name of your columns
Do like this
insert into data_temp (`col1`, `col2`) values (null, '{"test":
{"id":12,"otherdata":"x","otherdata2":"y"}}'); SELECT LAST_INSERT_ID();
In place of col1and col2 put your columns name then it will work for you.
I need to do batch MYSQL insert/updates. I got batch insert statement to work, but when the insert comes as multiple one liners it does not.. Similarly I have not been able to generate a batch update. Please see examples below.
Batch insert statement works
$sql = "INSERT INTO `test` (`somefield`) VALUES ('test', 'test');";
db::statement($sql);
Multiple separate insert statements NOT working
$sql = "INSERT INTO `test` (`somefield`) VALUES ('test'); INSERT INTO `test` (`somefield`) VALUES ('test');";
db::statement($sql);
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 'INSERT INTO test (somefield) VALUES ('test')' at line 1 (SQL: INSERT INTO test (somefield) VALUES ('test'); INSERT INTO test (somefield) VALUES ('test');)
Batch update statement not working
$sql = "INSERT INTO 'flights' (`id`, `airline`) VALUES ('142832', 'BA') ON DUPLICATE KEY UPDATE `airline`=VALUES(`airline`);"
db::statement($sql);
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 ''flights' (id, airline) VALUES ('142832', 'BA') ON DUPLICATE KEY UP' at line 1
Reviewed multiple Stackoverflow posts - but I am getting something wrong
Multiple insert statements - Multiple SQL Update Statements in single query
Batch update statement - Multiple Updates in MySQL
Would appreciate help on this - thanks!
This is weird. at a quick glance it appears that the batch statement is wrong, which is supposedly working.
A semantically correct batch statement would have the brackets separate each row of data, ie. like this:
INSERT INTO test
VALUES
('test1')
, ('test2')
, ('test3');
The separate insert statements look fine, however, your database driver might not support multiple statements in its statement method (most don't, AFAIK). The work around would be to start a transaction from your client, loop through the array of statements and execute. Then when all the statements execute, commit if there were no errors, or roll back the transaction. The first option is faster though.
The update statement doesn't work because the tablename flights is quoted using single-quotes. If you want to quote schema / table / column identifiers, use back-ticks, and reserve single-quotes for string values & dates, as you have done elsewhere in the same query. It is only necessary to escape a database element name if it is a reserved word, but naming database elements things like 'into', 'user', etc. is bad practice and should be avoided.
INSERT INTO flights (`id`, `airline`)
VALUES
('142832', 'BA')
ON DUPLICATE KEY UPDATE
airline=VALUES(`airline`)
Try to unquote flights, or quote it with back ticks in your last query (the batch update statement).
SQL query i'm running in phpMyAdmin :
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES ([1],[Example Company],[image.jpg])
phpMyAdmin generates this, i'm just changing the values.
SQL database:
1 companyId int(100)
2 companyName varchar(100)
3 companyImage varchar(100)
Error 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 '[1],[Example Company],[image.jpg])' at line 1
Use quotes, not brackets, around strings. Integers and floats don't require anything but MySQL allows quotes around those as well.
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES (1, 'Example Company', 'image.jpg')
Use backticks to escape column and table name.
Use quotes to limit strings.
Use nothing to limit numbers.
Don't use brackets at all in MySQL.
I'm facing a problem with select inside insert statements.
I've take a look at the questions similar to this but still the query is not working.
first, I'm working with MySQL version 5.6.24 with engine InnoDB, and I'm trying to insert this row:
INSERT INTO form (SELECT course_name FROM course WHERE course_id ='1' ), '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad')
I want the first column to be retrieved (which is only one value), but not the other.
I've tried many syntax formats, with VALUES, with semicolon, with more parentheses, etc... but non work. Here is 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 ' '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','no' at line 1
Thanks.
Use an INSERT-SELECT:
INSERT INTO form
SELECT course_name, '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad'
FROM course
WHERE course_id = '1'
I am trying to insert data from a csv file into mysql using BigDump.
It stops on line 2, with the error:
"Query: INSERT INTO location VALUES
(1,"O1","","","",0.0000,0.0000,, );
MySQL: 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 ' )' at line 1"
If I run the statement from withing phpmyadmin, it says:
"#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 ' )' at line 1"
What can I do to make the data get into the database?
Thank you.
The commas with no values looks sketchy to me.
INSERT INTO location VALUES (1,"O1","","","",0.0000,0.0000,, );
should probably be
INSERT INTO location VALUES (1,"O1","","","",0.0000,0.0000,NULL,
NULL);
Does your insert statement contain values for every column in the table? If not, you have to name the columns.
For example,
insert into location (col1, col2, col3) values (1, 2, 3);
If you show us the structure of the LOCATION table you can get better answers.