PDA

View Full Version : problem creating a mysql database



TonyB
19th August 2009, 15:16
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?

caduel
19th August 2009, 16:30
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

TonyB
20th August 2009, 10:11
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.

TonyB
20th August 2009, 10:50
For the record, removing the prepare() and executing the statement directly fixed the problem. Thanks again.

repaco
7th July 2010, 12:33
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:


void MainWindow::creadb()
{
QString db_conti = "conti1";

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword(ui->password->text());
if (db.open())
{
QSqlQuery query;
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.

saa7_go
15th July 2010, 19:20
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?

repaco
20th July 2010, 10:56
Hi saa7_go,
sorry delay in answer, I was on holiday.

My complete code is in my previous post.
Row 13

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

saa7_go
20th July 2010, 12:19
Are you sure that this part of your code




void MainWindow::creadb()
{
.....
QSqlQuery query;
query.exec("create database "+db_conti);

qWarning()<<query.lastError();
.....
}



prints the warning? Because, you don't do prepare statement like TonyB did.



QSqlQuery qm(maindb);
QString qs = QString("CREATE DATABASE %1").arg(dbName);
qm.prepare (qs);
if (!qm.exec()) {
....
}


I've tried your code. No error or warning occurs.

repaco
20th July 2010, 15:13
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.

saa7_go
21st July 2010, 01:30
I think it is a bug (http://bugreports.qt.nokia.com/browse/QTBUG-4048). If you want to fix the problem, use Qt 4.6.0 or above.

repaco
23rd July 2010, 15:39
many thanks.

Ciao.