problem creating a mysql database
Using Qt4.5.2 in Suse 11.1
I am trying to create a mysql database with the following code:
QSqlDatabase maindb = QSqlDatabase::addDatabase("QMYSQL", "main");
maindb.setDatabaseName ("mysql");
maindb.setHostName (url.host());
maindb.setUserName (url.user());
maindb.setPassword (url.pass());
if (!maindb.open()) {
qFatal("opening maindb");
}
QSqlQuery qm(maindb);
QString qs = QString("CREATE DATABASE %1;").arg(dbName);
qm.prepare (qs);
if (!qm.exec()) {
buildError (<snip>);
rc = 1;
}
maindb.close();
QSqlDatabase::removeDatabase (maindb.connectionName());
The exec() call fails with no useful information in the returned QSqlError. The mysql log shows only the following:
090819 14:34:20 8 Connect tonyb@localhost on mysql
8 Init DB mysql
8 Query SET NAMES utf8
8 Quit
i.e. the create database statement never appears.
This code, with the same userid, works okay in Qt3, so it's not a permissions problem. The log there shows
18 Connect tonyb@localhost on mysql
18 Init DB mysql
18 Query CREATE DATABASE KMyMoney
18 Quit
Any ideas? Workarounds?
Re: problem creating a mysql database
You should check every statement that could fail (prepare() can fail, too, for example).
Also, try removing the semi-colon ";" at the end of the query.
(I remember that was a problem for me once, I was using Oracle, though.)
HTH
Re: problem creating a mysql database
Many thanks; it was the prepare that was failing with message
Query error No 1295: This command is not supported in the prepared statement protocol yet QMYSQL3: Unable to prepare statement
Now to find a workaround.
Re: problem creating a mysql database
For the record, removing the prepare() and executing the statement directly fixed the problem. Thanks again.
Re: problem creating a mysql database
Quote:
Originally Posted by
TonyB
For the record, removing the prepare() and executing the statement directly fixed the problem. Thanks again.
Hi,
I'm using on linux
Mysql 5.0.77-1
QT Creator 1.3.1
QT Libs 4.5.1 (due to Distro compatibility)
This is my code to create DB Schema:
Code:
void MainWindow::creadb()
{
db.setHostName("localhost");
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword(ui->password->text());
if (db.open())
{
query.exec("create database "+db_conti);
qWarning()<<query.lastError();
... code for creating tables, views and users...
db.close();
}
}
I'm getting with qWarning() the same your error:
QSqlError(1295, "QMYSQL3: Unable ro prepare statement", "This command is not supported in the prepared statement protocol yet")
But DB Schema is created. Also tables views and users are created without problems.
Do I ignore that error?
Thanks.
Re: problem creating a mysql database
Quote:
Originally Posted by
repaco
I'm getting with qWarning() the same your error:
QSqlError(1295, "QMYSQL3: Unable ro prepare statement", "This command is not supported in the prepared statement protocol yet")
But DB Schema is created. Also tables views and users are created without problems.
Do I ignore that error?
Thanks.
Can you post your codes where the warning occurs here?
Re: problem creating a mysql database
Hi saa7_go,
sorry delay in answer, I was on holiday.
My complete code is in my previous post.
Row 13
Code:
query.exec("create database "+db_conti);
db_conti contains DB Schema Name
and row 15 to get output log about this command.
DB Schema is created, but query.lastError() gives me error on create database. :confused:
thanks in advance
Re: problem creating a mysql database
Are you sure that this part of your code
Quote:
Originally Posted by
repaco
Code:
void MainWindow::creadb()
{
.....
query.exec("create database "+db_conti);
qWarning()<<query.lastError();
.....
}
prints the warning? Because, you don't do prepare statement like TonyB did.
Code:
qm.prepare (qs);
if (!qm.exec()) {
....
}
I've tried your code. No error or warning occurs.
Re: problem creating a mysql database
Hi again,
As ToniB wrote, using qm.prepare (qs), no DB Schema is created, with non evidence of error.
ToniB suggested to remove the prepare() and execute the statement directly.
Executing directly, DB Schema is created, but I get error.
It is not a big problem, since DB is created, but I'd like, if it is possible, to fix the problem.
Tks for answer.
Re: problem creating a mysql database
I think it is a bug . If you want to fix the problem, use Qt 4.6.0 or above.
Re: problem creating a mysql database