how to solve unexpected token using MySQL Hibernate? - mysql

Im getting below error
org.hibernate.hql.internal.ast.QuerySyntaxException:unexpected token values: values near line 1, column 161 .
[insert into shop_information(storeName, ownername, license, email, dlnumber, gst, pan, pincode, phonenumber, mobile, fax, cst, phone, district, state, country) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]

You have to use
session.createSQLQuery(SQL_QUERY);
not
session.createQuery(SQL_QUERY);
HQL only supports insert from another table.
e.g.
INSERT INTO Entity properties_list select_statement.
If you wan't to use standard insert statement like
INSERT INTO Table properties_list values ( :vals)
you need
session.createSQLQuery(SQL_QUERY)
Edit, to answer the comment:
I think that the parameters are 0-based, so try to start the parameters from 0:
query.setParameter(0,storeName);
query.setParameter(1,ownerName);
etc...
However the better way is to use named parameters:
sessionFactory.getCurrentSession()
.createSQLQuery("update table set field = 1 where id = :id")
.setParameter("id", someId)
.executeUpdate();

Related

What if too many parameters in mysql query in node.js

This table has many parameters, and when i do insert it's like this, (... is for demo propose)
const sqlCmd = `insert into Consumer (key, secret, ..., version)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
try {
const param = [myTable.key, myTable.secret, ..., myTable.version];
return await dbPool.execSqlCmd(sqlCmd, param)
}
Is there a way to avoid so many ?s ?
In SQL, you use one ? parameter placeholder for each scalar value you want to pass to the SQL statement. No more, no less.
If you want fewer ? placeholders, then you must pass fewer parameters. You can do this by:
Omitting some columns from the INSERT statement. The row inserted will use the DEFAULT value declared for the column, or else NULL if there is no default.
Using a constant expression in the VALUES clause for some columns, instead of a parameter. I mean this can be a constant literal, like a numeric or string or date, or it can be an expression or function, which will be evaluated, and the resulting value used for the INSERT.

rails 4.34 mysql INSERT query doesnt work

My rails code is something simple like (code and sql output changed to make it generic)
Router.new(
:name => params[:data][:name],
:subid => params[:data][:subid],
:info => params[:data][:info],
:parent => #parent)
Rails generated INSERT sql
INSERT INTO `mydb` (`name`, `subid`, `info`, `parent_id`, `created_at`, `updated_at`)
VALUES (?, ?, ?, ?, ?, ?) [["name", "myname"], ["subid", ""], ["info", ""], ["parent_id", 2], ["created_at", "2016-09-09 06:06:37"], ["updated_at", "2016-09-09 06:06:37"]]
This is the error I am getting:
Mysql::Error: Incorrect integer value: '' for column 'parent_id' at row 1: INSERT INTO mydb (name, subid, info, parent_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)
Looks like mysql doesn't like this type of syntax. I have version 5.6.32.
The values look work when I modify the command to the values directly into "VALUES". Is there a way to go back to the format where the values are inserted at "VALUES" or get the insert working without having to recreate the INSERT statement.
EDITED: I think it is not the actual the SQL. Is there a way to print the actual SQL?

Issue in inserting values to table through jsp

I got this exception while using one of the answers from stackoverflow.
You can't specify target table 'tablename' for update in FROM clause
This is my query and am using JSP to pass queries:
String queryString = "INSERT INTO tablename(SL_No,candidate,phone,pan,mailid)
VALUES(SELECT (MAX(SL_No)+1 newSL_No from tablename), ?, ?, ?, ? ))";
Thanks in advance.
INSERT INTO tablename(SL_No,candidate,phone,pan,mailid)
select MAX(SL_No) + 1, ?, ?, ?, ?
from tablename
But actually it looks like you could just use the auto-increment of the SQL engine to do MAX(SL_No) + 1. If you change to column to that then your statement would be
INSERT INTO tablename(candidate,phone,pan,mailid)
values (?, ?, ?, ?)

Unknown error in bind_param in mysql

Hi i got this error message on mysql query which has taken a toll of my time.
Fatal error: Uncaught Error: Call to a member function bind_param() on unknown in ...
Anyone encounter this unknown error before?
How can mysql display the error?
Below is my query:
($db->prepare('INSERT INTO program_list(
PROGRAM_CODE_ID, MONTH, PDATE, HOURS, PUNIQUE, AUNIQUE,
EYEBALL, LEVEL, REMARK, REVENUE, TRAINER_ID, CLIENT_ID, ADMIN_ID, UPD_TIME) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')) || fail('MySQL prepare', $db->error);
$stmt->bind_param('ssssssssssssss', $program_code, $month, $pdate, $hours, $unique, $adult, $eyeball, $lvl, $remark, $revenue, $trainer_id, $client_id, $admin_id, $time) || fail('MySQL bind_param', $db->error);
I did the echo of the query with variables and able to execute the query successfully on phpmyadmin.

Syntax error mysql

Where is the syntax error in this statement? I dont get it, can I use the "? bind param method" in this case? I use PDO
$stmt = $dbh->prepare("INSERT INTO epinfo WHERE TVShowTitle=? (Season, Episode, SDLink, HDLink, DlSDLink, DlHDLink) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bindParam(1, $_POST[tvshow]);
$stmt->bindParam(2, $_POST[season]);
$stmt->bindParam(3, $_POST[episode]);
$stmt->bindParam(4, $_POST[sdlink]);
$stmt->bindParam(5, $_POST[hdlink]);
$stmt->bindParam(6, $_POST[dlsdlink]);
$stmt->bindParam(7, $_POST[dlhdlink]);
$stmt->execute();
Error message:
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 'WHERE TVShowTitle='Breaking Bad' (Season, Episode, SDLink, HDLink, DlSDLink, DlH' at line 1
The standard INSERT syntax has no WHERE clause. I think you want to UPDATE your existing record.
UPDATE epinfo
SET Season = ?,
Episode = ?,
SDLink = ?,
HDLink = ?,
DlSDLink = ?,
DlHDLink = ?
WHERE TVShowTitle=?
INSERT INTO epinfo (TVShowTitle, Season, Episode, SDLink, HDLink, DlSDLink, DlHDLink) VALUES (?, ?, ?, ?, ?, ?, ?)"
You need to remove the where clause.
from here
INSERT INTO epinfo WHERE
Try like this:-
INSERT INTO epinfo(Season, Episode, SDLink, HDLink, DlSDLink, DlHDLink)
VALUES (?, ?, ?, ?, ?, ?)
From your comments:
If you want to update the value then use it like this:-
update epinfo
set column = "value"
where TVShowTitle=?