PDA

View Full Version : Execute a .sql file with more than one query



ShadowBelmolve
18th August 2010, 03:48
Hello, I'm trying to do anything like this



QSqlQuery *query = new QSqlQuery();
QFile file("querys.sql");
file.open(QFile::ReadOnly|QFile::Text);
query.exec(file.readAll());


//querys.sql
CREATE TABLE foo(id int, name varchar);
CREATE TABLE bar(id int, name varchar);
....


but only the first query is executed :(

Lykurg
18th August 2010, 06:57
normally all should be executed, so please debug if the file is read correctly and post the error from your sql driver.

ShadowBelmolve
18th August 2010, 16:58
strange, the file is read correctly and query.lastError().text() is empty.

I've tested a query.exec() with 2 inserts but only the first is executed.

I think that query.exec() only execute one query per time :(

saa7_go
18th August 2010, 19:07
After you read all text from file, you split all sql statements by semicolon. Then you can execute every sql statement one by one.

Urthas
19th August 2010, 00:12
I should think you would also be able to loop through the file contents and execute each query iteratively.

Lesiok
19th August 2010, 07:01
Maybe it is SQL driver dependent. On PostgreSQL it is working.

ShadowBelmolve
19th August 2010, 16:48
yeah, in ruby the driver exec only one query per time.

Now I'm spliting the file and running a query per exec. Thx to all :)