hibernate + mysql + load data in file

ぐ巨炮叔叔 提交于 2019-12-20 06:47:45

问题


Hi I am trying to load data from a file to Mysql DB using hibernate.

here is the query,

session.createSQLQuery("LOAD DATA INFILE E:/uploaded/NumSerie/NS/NumSerie.txt INTO TABLE prod CHARACTER SET latin1 FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;").executeUpdate();

But i get the following error,

org.hibernate.QueryException: Space is not allowed after parameter prefix ':' [LOAD DATA INFILE E:/uploaded/NumSerie/NS/NumSerie.txt INTO TABLE prod CHARACTER SET latin1 FIELDS TERMINATED BY ';' LINES TERMINATED BY '
' IGNORE 1 LINES;]
at org.hibernate.engine.query.ParameterParser.parse(ParameterParser.java:92)
at org.hibernate.engine.query.ParamLocationRecognizer.parseLocations(ParamLocationRecognizer.java:75)

How can I rewrite this query so this is executed properly?

Thanks in advance!


回答1:


Try creating a parameterised query

I'm no Hibernate guru but this could work:

session.createSQLQuery("LOAD DATA INFILE :file INTO TABLE prod CHARACTER SET latin1 FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;")
    .setString("file", "E:/uploaded/NumSerie/NS/NumSerie.txt")
    .executeUpdate();


来源:https://stackoverflow.com/questions/10351078/hibernate-mysql-load-data-in-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!