PDA

View Full Version : create database if not exist



snaderi
29th June 2013, 15:09
hello,I create an app that use for save data,but i have an problom!I want when my app execute on other computer for the frist time,automatically create databse on that comuter if not exist! please help me!!

saman_artorious
29th June 2013, 15:48
I don't think this is qt relevant question. check the format of the query regarding to the DBMS you are using, CREATE TABLE IF NOT EXISTS is for mysql I guess.

snaderi
29th June 2013, 16:08
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 when I execute my app another computer.I want my db create automatically!

saman_artorious
29th June 2013, 19:00
There should of course be api functions relevant to db_create or db_query and alike. Go through my sql c api functions and execute either the create function or the query function and write the query i mentioned above as a parameter.

ChrisW67
29th June 2013, 23:33
You will need a MySql privileged user account, connect to the mysql system database as that user, execute a "create database" statement, statements to create tables, and statements to create users and/or grant privileges. Then disconnect as the privileged user, connect as the usual minimally privileged user to continue using your new database.