I am using MYSQL 5.1, I am trying to write an insert statement in my java class. It sounds very simple but i tried and getting exception. I am sure there is a syntax error with my code. below is the snippet could any one help me out here.
ID is in primary key
atm_ID is varchar
trasn_id is varchar
sysDate is Date
rest is int
Please help me out.
Error:
SQL statement is not executed!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 '£10, £20, £50, £100, trans_Id) VALUES (7,'hello','hello','2','2','2','2','2','he' at line 1`
Code:
String query = "INSERT INTO atm_data (ID,atm_Id, sysDate, availBalance, £10, £20, £50, £100, trans_Id) VALUES (7,'hello','2010-09-15 01:20:06','2','2','2','2','2','hello')";
It looks like you simply need to escape the column names that start with £ with backticks:
INSERT INTO atm_data (ID, ... `£10`, `£20`, `£50`, `£100` ...
Test case:
CREATE TABLE tb (`£10` int);
Query OK, 0 rows affected (0.05 sec)
INSERT INTO tb (£10) VALUES (10);
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 '?10) VALUES (10)' at line 1
INSERT INTO tb (`£10`) VALUES (10);
Query OK, 1 row affected (0.00 sec)
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'm creating a stored procedure using phpmyadmin for mysql 5.5.55 .
INSERT INTO drama_ (value1,value2,value3,value4,value5,value6)
VALUES (val1,val2,0,0,0,0);
SELECT LAST_INSERT_ID();
but i'm getting a syntax error in SELECT LAST_INSERT_ID();
EDIT :
You have an error your SQL syntax: check the manual that corresponds to your MYSQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()' at line 4
I have a mysql table school
id dob name surname
1 03.04.2011 jj
2 14.07.1999 na
.. ............ ..
I have many rows of data in the above table. Now what I want to do is; fill the surname column using an insert clause as follows
INSERT INTO `school` (surname) VALUES
('highvision'),
('oceanof'),
('malindimetho'),
('tahdhibprima'),
('stpatricks'),
...............
('stpatricks');
note: the number of rows I am inserting equals the number of rows in my table
On using the above INSERT statement I get 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 '' at line ..
How can I insert the rows?
You cannot use INSERT to fill the table.
You can use the following:
UPDATE `school` SET `surname` = 'highvision' WHERE `id` = '1'
I am trying to run this SQL Query:
INSERT INTO controldata (field,value)
VALUES (customer_billing_product_type,
SELECT name from customer_billing_product_types)
to insert all rows in the customer_billing_product_types table into the controldata table with the field always being customer_billing_product_type but i get an SQL Error saying:
#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
'SELECT name from customer_billing_product_types)' at line 1
The INSERT as you wrote it expects to insert one record, but then in values an entire set of records is returned. Try to change your query to
INSERT INTO controldata(field, value)
SELECT customer_billing_product_type, t.name FROM customer_billing_product_types t;
This selects all t.name values from customer_billing_product_types, adds customer_billing_product_type as column and inserts all the results into controldata.
You are using wrong syntax try below:
INSERT into controldata (field1) SELECT name from customer_billing_product_types;
If you want to keep a field as constant then you can use as per below:
INSERT into controldata (field,value) SELECT 'customer_billing_product_types',name from customer_billing_product_types;
Why does the following show a mysql 1064 error?
I have a the following table:
daily_note (id, time (timestamp), rate_id, note )
On this table I'm executing the following insert:
INSERT INTO daily_note (note)
VALUES ('this is a note')
WHERE rate_id = 37
AND time > '2011-05-22 00:00:00'
Cannot perform insert:
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 'WHERE rate_id = 37 AND time > '2011-05-22 00:' at line 3'
You can't INSERT using a WHERE statement. If you want to modify existing records based on some condition/criteria, use an UPDATE statement instead of INSERT.
INSERT with WHERE makes no sense.
INSERT creates new rows, whereas WHERE specifies criteria for retrieving/updating existing rows.
Read up about the syntax and meaning of the statement that you're trying to use, before trying to use it.
Code should read:
UPDATE DAILY_NOTE
SET field='this is a not'
WHERE rate_id = 37
AND time > '2011-05-22 00:00:00'