QT でデータベース接続をテストしていますが、このエラーが発生します
[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
これはコードです:
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>
using namespace std;
int main() {
cout << "SHELL = " << getenv("SHELL") << endl;
cout << "SHELL = " << getenv("TERM") << 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() << "Failed to connect to mysql as user testuser";
return db;
}
void Database::closeDb(){
QSqlDatabase::removeDatabase("QMYSQL");
}
何か案が???
アップデート:
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>
using namespace std;
void conn() {
cout << "SHELL = " << getenv("SHELL") << endl;
cout << "SHELL = " << getenv("TERM") << endl;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("xxxxxxxx");
db.setDatabaseName("xxxxxxxx");
db.setUserName("xxxxx");
db.setPassword("xxxxxxxxxx");
if (!db.open()) qDebug() << "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&)'
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&, QString const&)'
/usr/local/dev/env/test.cpp:15: undefined reference to `QSqlDatabase::setHostName(QString const&)'
/usr/local/dev/env/test.cpp:16: undefined reference to `QSqlDatabase::setDatabaseName(QString const&)'
/usr/local/dev/env/test.cpp:17: undefined reference to `QSqlDatabase::setUserName(QString const&)'
/usr/local/dev/env/test.cpp:18: undefined reference to `QSqlDatabase::setPassword(QString const&)'
/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
アップデート:
これはうまくいきました..
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>
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() << "Failed to connect to mysql as user testuser";
}
else {
qDebug() << "hello";
}
}
void closeDb() {
QSqlDatabase::removeDatabase("QMYSQL");
}
int main() {
cout << "SHELL = " << getenv("SHELL") << endl;
cout << "SHELL = " << getenv("TERM") << endl;
conn();
return 0;
}
私は追加しなければなりませんでした
QT += core gui sql
プロジェクトファイルに..