0

Is there an existing library/framework that I can leverage on that will allow me to generate an INSERT mysql scripts from a query result.

For example, in HQL I have:

SELECT book FROM book b WHERE b.created >= :today

Instead of a List<Book>, what I'll have are the texts:

INSERT INTO book VALUES (...)

Or anyway I can convert list of hibernate entity into a mysql script INSERT INTO script.

Any assistance will be appreciated.

Thank you!

2 Answers 2

1

You could write a SELECT query like this:

SELECT 'INSERT INTO book (id, name, isbn) VALUES ('|| b.id ||','|| b.name ||','|| b.isbn ||');' FROM book b;

You need to pay attention to VARCHAR column, because the values need to be escaped with quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

this looks good, thanks, though it's returning null, but when I use concat() it works fine.
For null values you can use NULLIF.
0

You can use mysqldump for this:

mysqldump -u username -p dbname book --compact --no-create-info --where="created >= '2014-10-01'" > dump.sql

This will generate the INSERT INTO statements for table rows matching the WHERE clause book.created >= '2014-10-01'

3 Comments

I'll be needing it via java code. And I need to not the whole database, I just want the create statements for the result of my query.
It will return only the selected rows of a single table
sorry missed that you have a where clause. but i'll be needing it via java code where the return will be a List<String> insertStatements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.