=== 測試環境
OS: Windows 7
Qt Verstion: 5.5.0
Compiler: Visual Studio 2013
OS: Arch Linux 4.1.6
Qt Version: 5.5.0
Compiler: GCC 5.2.0
=== 程式範例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); | |
db.setHostName("127.0.0.1"); | |
db.setDatabaseName("FirstDB"); | |
db.setUserName("demo"); | |
db.setPassword("password"); | |
bool ok = db.open(); | |
if (ok) | |
{ | |
QSqlQuery query("SELECT \"SID\", \"Account\", \"CreateDate\", \"LoginTimes\" FROM \"Account\""); | |
while (query.next()) | |
{ | |
qWarning() << query.value(0).toString(); | |
qWarning() << query.value(1).toString(); | |
qWarning() << query.value(2).toString(); | |
qWarning() << query.value(3).toString(); | |
} | |
qDebug() << query.lastError().text(); | |
} |
QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
在 Windows,確認下列檔案
MyAppDir\sqldrivers\qsqlpsql.dll
MyAppDir\libpq.dll
MyAppDir\libeay32.dll
MyAppDir\ssleay32.dll
在 linux,確認 qt project file 加上
INCLUDEPATH+=/usr/include/pgsql
LIBS+=-L/usr/lib -lpq
=== 資料來源
Qt Documentation - SQL Database Drivers
http://doc.qt.io/qt-5/sql-driver.html
Qt: SELECT-Query to PostgreSQL-Server always responses with NULL
http://stackoverflow.com/questions/13633055/qt-select-query-to-postgresql-server-always-responses-with-null
QT5: Failed to load psql driver in windows
http://stackoverflow.com/questions/20884010/qt5-failed-to-load-psql-driver-in-windows
沒有留言:
張貼留言