0

我正在测试 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

到项目文件..

4

1 回答 1

0

您正在返回您QSqlDatabaseint main()函数,这是无效的,因为QSqlDatabaseis not int。你为什么要退货?例如,如果您希望返回类型描述成功,则使用-1and 否则0

至于第二个错误,您没有定义任何类,Database因此您无法定义方法Database::closeDb()。而是创建一个函数来做你想做的事:

void closeDb() {
  QSqlDatabase::removeDatabase("QMYSQL");
}

但是我看不出closeDb()你不使用它的目的。

于 2012-08-16T10:41:46.713 回答