title: qt mysql connection error
body: <p>i am testing out a database connection on QT but i get this error</p>
  
  <pre><code>[root@server env]# make
  g++ -c -m64 -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../lib64/qt4/mkspecs/linux-g++-64 -I. -I../../../include/QtCore -I../../../include/QtGui -I../../../include -I. -I. -o test.o test.cpp
  test.cpp: In function ‘int main()’:
  test.cpp:20: error: cannot convert ‘QSqlDatabase’ to ‘int’ in return
  test.cpp: At global scope:
  test.cpp:23: error: ‘Database’ has not been declared
  make: *** [test.o] Error 1
  </code></pre>
  
  <p>this is the code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include "/usr/include/mysql/mysql.h"
  #include &lt;QDebug&gt;
  #include &lt;QtSql/QtSql&gt;
  
  using namespace std;
  
  int main() {
  
      cout &lt;&lt; "SHELL = " &lt;&lt; getenv("SHELL") &lt;&lt; endl;
      cout &lt;&lt; "SHELL = " &lt;&lt; getenv("TERM") &lt;&lt; endl;
  
      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("xxx.xxxx.xxxx.xxx");
      db.setDatabaseName("SAMS");
      db.setUserName("xxxxxxxx");
      db.setPassword("xxxxxxxxxx");
      if (!db.open()) qDebug() &lt;&lt; "Failed to connect to mysql as user testuser";
      return db;
  }
  
  void Database::closeDb(){
      QSqlDatabase::removeDatabase("QMYSQL");
  }
  </code></pre>
  
  <p>any idea???</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include "/usr/include/mysql/mysql.h"
  #include &lt;QDebug&gt;
  #include &lt;QtSql/QtSql&gt;
  
  using namespace std;
  
  void conn() {
  
      cout &lt;&lt; "SHELL = " &lt;&lt; getenv("SHELL") &lt;&lt; endl;
      cout &lt;&lt; "SHELL = " &lt;&lt; getenv("TERM") &lt;&lt; endl;
  
      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("xxxxxxxx");
      db.setDatabaseName("xxxxxxxx");
      db.setUserName("xxxxx");
      db.setPassword("xxxxxxxxxx");
      if (!db.open()) qDebug() &lt;&lt; "Failed to connect to mysql as user testuser";
  }
  
  void closeDb(){
      QSqlDatabase::removeDatabase("QMYSQL");
  }
  
  
  
  [root@server env]# make
  g++ -c -m64 -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../lib64/qt4/mkspecs/linux-g++-64 -I. -I../../../include/QtCore -I../../../include/QtGui -I../../../include -I. -I. -o test.o test.cpp
  g++ -m64 -Wl,-O1 -o env test.o     -lQtGui -lQtCore -lpthread 
  /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In function `_start':
  (.text+0x20): undefined reference to `main'
  test.o: In function `closeDb()':
  /usr/local/dev/env/test.cpp:23: undefined reference to `QSqlDatabase::removeDatabase(QString const&amp;)'
  test.o: In function `QString':
  /usr/include/QtCore/qstring.h:683: undefined reference to `QSqlDatabase::defaultConnection'
  test.o: In function `conn()':
  /usr/local/dev/env/test.cpp:14: undefined reference to `QSqlDatabase::addDatabase(QString const&amp;, QString const&amp;)'
  /usr/local/dev/env/test.cpp:15: undefined reference to `QSqlDatabase::setHostName(QString const&amp;)'
  /usr/local/dev/env/test.cpp:16: undefined reference to `QSqlDatabase::setDatabaseName(QString const&amp;)'
  /usr/local/dev/env/test.cpp:17: undefined reference to `QSqlDatabase::setUserName(QString const&amp;)'
  /usr/local/dev/env/test.cpp:18: undefined reference to `QSqlDatabase::setPassword(QString const&amp;)'
  /usr/local/dev/env/test.cpp:19: undefined reference to `QSqlDatabase::open()'
  /usr/local/dev/env/test.cpp:19: undefined reference to `QSqlDatabase::~QSqlDatabase()'
  /usr/local/dev/env/test.cpp:19: undefined reference to `QSqlDatabase::~QSqlDatabase()'
  /usr/local/dev/env/test.cpp:14: undefined reference to `QSqlDatabase::~QSqlDatabase()'
  collect2: ld returned 1 exit status
  make: *** [env] Error 1
  </code></pre>
  
  <p><strong>update:</strong></p>
  
  <p>this worked..</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include "/usr/include/mysql/mysql.h"
  #include &lt;QDebug&gt;
  #include &lt;QtSql/QtSql&gt;
  
  using namespace std;
  
  void conn() {
  
      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("xxxxxxxxxxx");
      db.setDatabaseName("SAMS");
      db.setUserName("xxxxxxxx");
      db.setPassword("xxxxxxxx");
      if (!db.open()) {
          qDebug() &lt;&lt; "Failed to connect to mysql as user testuser";
      }
      else {
          qDebug() &lt;&lt; "hello";
      }
  }
  
  void closeDb() {
      QSqlDatabase::removeDatabase("QMYSQL");
  }
  
  int main() {
  
      cout &lt;&lt; "SHELL = " &lt;&lt; getenv("SHELL") &lt;&lt; endl;
      cout &lt;&lt; "SHELL = " &lt;&lt; getenv("TERM") &lt;&lt; endl;
      conn();
      return 0;
  
  }
  </code></pre>
  
  <p>i had to add</p>
  
  <pre><code>QT += core gui sql
  </code></pre>
  
  <p>to the project file..</p>