Create database
-
Hi snaderi.
Which database are you using?
Maybe you can verify if database file exists ( or connection it's possible) at startup and if not, call a function to create database.
Regards. -
Did you verify if other computer can accest to mysql server?
How do you are connecting to mysql server? Can you put a little part of your code here?
Regards -
thank you
my connecting file cantains this code:
@bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("sa2");
db.setUserName("root");
db.setPassword("");if (!db.open()) { QMessageBox::critical(0,QObject::tr("Database Error"), db.lastError().text()); return false; } return true;
}@
I have only one problem and thats is I forced to create db manually.I want my db create automatically!
-
Well, you must to use sql commands to do this, and some modifications on your code.
Try this
@ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("sdg2101zmv");if (!db.isValid()){ qDebug() << db.lastError().text(); return; } db.open(); QString query = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'sa2'"; QSqlQuery q = db.exec(query); if (q.size() == 0){ db.exec("CREATE DATABASE IF NOT EXISTS sa2;"); qDebug() << db.lastError().text(); db.setDatabaseName("sa2"); bool yourApp::createTables(); }
@
Where
@
bool yourApp::createTables();
@is necesary if you need create tables to your new database.
First quere count all databases named "sa2".
Regars. -
Sure. You must to ajust my example to your code.
In this case replace line 8 to
@return false;
@Or don't return and put some message to app user.
Regards.