PDA

View Full Version : Mysql unknown database, QMYSQL unable to connect



lixo1
10th July 2010, 22:05
Dear all,

I just downloaded Qt 4.6.1 installer for windows (VC++ 2008), and I would like to create and use QMYSQL.
I tried all sql examples with QSQLITE and they worked, then I downloaded MySql 5 installer, I built the *.dll qt plugin,
but I can't open/create a new database from scratch.
My code is the following:



QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("pass");
db.setDatabaseName("test.mysql"); //I would like to create a new database, locally on my pc
if (!db.open()) return false;


And I'm getting: Unknown database 'test.mysql' QMYSQL: unable to connect.

Any kind of help is appreciated,
Thank you very much.

Lykurg
10th July 2010, 23:25
You have to exec an valid SQL statement to create a new database, afterwards you can set it.
CREATE DATABASE foo;

saa7_go
10th July 2010, 23:29
If you use QMYSQL driver,

db.setDatabaseName("db_name");
means open existing database.

You can create database using QSqlQuery.

QSqlQuery query("CREATE DATABASE db_name");

lixo1
14th July 2010, 21:39
Thank you very much!