PDA

View Full Version : Database not being created



Splatify
4th February 2011, 00:21
Hi all I have been trying for hours now to get a MySQL database to work in my project. Only problem is I am unable to create the database. I don't get any errors anything, nothing seems to happen. Everything compiles fine but no databases are created. Qt tells me that I have all the database drivers i need...

I have: ("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC")

The code that I'm using to try and create my database is:



QSqlDatabase db;
db.addDatabase("QMYSQL");
db.setDatabaseName("test");
if(!db.open()){
message.information(this, "Error", "Database not opened");
}


What am I doing wrong? This is all beginning to annoy me now!!

Thanks for your time and trouble. Any help would be much appreciated :)

ChrisW67
4th February 2011, 00:31
Two things:

You need to provide the Mysql plugin with a host name, user name, and password. From the docs:

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigblue");
db.setDatabaseName("flightdb");
db.setUserName("acarlson");
db.setPassword("1uTbSbAs");
bool ok = db.open();
The plugin will not create the Mysql database if it does not exist.