2012-08-16 58 views
0

我測試了上QT數據庫連接,但我得到這個錯誤QT MySQL連接錯誤

[[email protected] 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"); 
} 

任何想法???

UPDATE:

#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"); 
} 



[[email protected] 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 

到項目文件..

回答

0

您正在返回你的QSqlDatabaseint main()功能,這是無效的,因爲QSqlDatabaseint。你爲什麼要回來?如果您希望返回類型描述成功,則使用-1,否則0

至於第二個錯誤,您沒有任何定義爲Database的類,因此您無法定義方法Database::closeDb()。相反,創建一個函數來完成你想要什麼:

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

但是當你不使用它反正我沒有看到closeDb()目的。

+0

感謝那個netrom,現在它給出了未定義的引用錯誤.. – krisdigitx 2012-08-16 10:51:36

+0

這是因爲你仍然需要你的'main main()'函數,因爲它是程序的入口點。未定義的引用錯誤意味着編譯器無法找到必要的頭/類信息。你需要確保你有'QSqlDatabase'等等。請注意,這在你發佈的第一個片段中沒有問題 - 所以你可能已經改變了編譯語句。 – 2012-08-16 10:54:51