Akash
Akash

Reputation: 19

Insert into table using string in MySql

CREATE TABLE IF NOT EXISTS tmp_park_log (
  uniqueid VARCHAR (20),
  parked_sec INT
) ;
DELETE 
FROM
  tmp_park_log ;
INSERT INTO tmp_park_log (uniqueid, parked_sec) 
SELECT 
  uniqueid,
  SUM(parked_sec) 
FROM
  park_log 
GROUP BY uniqueid ;

This is executing successfully in MySql: But, when i put this in a string and use Prepared Statement it gives following 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 'DELETE FROM tmp_park_log;  INSERT INTO tmp_park_log (uniqueid, parked_sec) SELEC' at line 1

Upvotes: 0

Views: 63

Answers (3)

asafm
asafm

Reputation: 921

Since you didn't attached the Java code I assume you are trying to d prepare statement for all the text and not to prepare every and execute it.

few hints:

  1. There is no need to prepare the CREATE TABLE statement you can just use create statement and execute it (as no bind variables are used) http://docs.oracle.com/javase/tutorial/jdbc/basics/tables.html
  2. If your application allows it , consider the use of truncate instead of delete, it will be faster and reduce the log generation.
  3. Don't forget to commit :).

BR

Upvotes: 0

vadchen
vadchen

Reputation: 1462

SQL syntax for prepared statements does not support multi-statements (that is, multiple statements within a single string separated by “;” characters).

See here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

Upvotes: 1

Lukas Warsitz
Lukas Warsitz

Reputation: 1239

In the first case you arent using one commando instead you use a few commandos and when you put it in a string as a prepared statement you must create one prepared statement for every single commando you want to execute.

Upvotes: 0

Related Questions