PDA

View Full Version : QSqlQuery does not execute



weixj2003ld
4th June 2012, 10:28
I want to insert many records into MySQL,so I do as followings:


...
QSqlQuery mQuery;
if(!mQuery.exec("LOAD DATA INFILE '"+fileName+"' INTO TABLE table1 FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\r\n'"))
{
QMessageBox::warning(0,"",mQuery.lastError().text());
return false;
}
...

and there is no respones,that is, table1 has no recorders and there is no error.

But when I execute " LOAD DATA INFILE '"+fileName+"' INTO TABLE table1 FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\r\n'" " in cmd window,it is ok,why?

chenz
4th June 2012, 11:12
You could look at mQuery.executedQuery() to see if the the query executed by QSqlQuery really matches the one you enter in the command window.

It could also be a transaction problem, maybe there is a COMMIT missing in your Qt code?

weixj2003ld
11th July 2012, 03:25
You could look at mQuery.executedQuery() to see if the the query executed by QSqlQuery really matches the one you enter in the command window.

I print the 'executedQuery()',and find that it execute ""LOAD DATA INFILE '"+fileName+"' INTO TABLE table1 FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\r\n'"))" ,that is, it exectue the query as I type.



It could also be a transaction problem, maybe there is a COMMIT missing in your Qt code?

I have no transaction ,so there is no COMMIT.

By the way, if I exectue 'Insert' or 'update',it is ok.

wysota
11th July 2012, 08:18
Does exec() return true or false? What does QSqlQuery::lastError() return?

weixj2003ld
11th July 2012, 10:35
return true;

wysota
11th July 2012, 11:50
Then the query was executed successfully and there is nothing more you can do from within your application.

Lesiok
11th July 2012, 12:13
Problem with incorrect file path ?

Lykurg
11th July 2012, 12:13
...beside escaping your string proper. I bet it must be
mQuery.exec("LOAD DATA INFILE '"+fileName+"' INTO TABLE table1 FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\r\\n'")Also have a look at bindValue or QString::arg() which makes your code better readable.