1

i wrote a program that need to connect to database to insert some data , my executable file not connect to database but when i checked with code connection established! i don't know what is problem , do you know what is problem? i use Qt to connect to database and my database is on mySql and here is how i connect :

soccer_db = QSqlDatabase::addDatabase("QMYSQL" , "sss");
    soccer_db.setHostName(addrrFile.c_str());
    soccer_db.setDatabaseName("sss");
    soccer_db.open();
    if (!soccer_db.open()){
        emit dsignal("ssss not opened. Ckech whether server is down or change config file");
        return false;
    }
László Papp
  • 51,870
  • 39
  • 111
  • 135
mari
  • 417
  • 1
  • 6
  • 21

2 Answers2

0

You are getting a failure because you try to open the database twice. The first attempt succeeds but the second one fails. Remove the first call to open, like this

soccer_db = QSqlDatabase::addDatabase("QMYSQL" , "sss");
soccer_db.setHostName(addrrFile.c_str());
soccer_db.setDatabaseName("sss");
if (!soccer_db.open()){
    emit dsignal("ssss not opened. Ckech whether server is down or change config file");
    return false;
}
john
  • 85,011
  • 4
  • 57
  • 81
  • i try this but nothing happened , when i try to execute binary file from terminal connection establish but when i double click on icon it fails! – mari Nov 24 '13 at 08:06
  • 1
    Do you need a username and password? There's lots of things that can be wrong when you try to connect to a database, you shouldn't sound so surprised. Try using `soccer_db.lastError()` to find out what the error is. – john Nov 24 '13 at 08:12
  • thank you for your andswer , problem was with hostName becuase i use relative address for reading database ip address ,qt change current directory when it run binary file and can't find the ip address – mari Nov 24 '13 at 09:40
0

i want to answer my question, in qt you should address your files completely not relative address, if you need to address completely you can give current directory by QtDir

mari
  • 417
  • 1
  • 6
  • 21