Qt로 Database에 접속하려고 하면 QSqlDatabase를 사용해야 합니다. 그 중 사용할 데이터베이스가 PostgreSQL일 경우 addDatabase("QPSQL")을 사용합니다.
간단히 코드를 보면 아래와 같습니다.
m_serverDB = QSqlDatabase::addDatabase("QPSQL"); // PostgreSQL Driver
m_serverDB.setHostName(m_connectionInfo.host);
m_serverDB.setPort(m_connectionInfo.port);
m_serverDB.setDatabaseName(m_connectionInfo.dbName);
m_serverDB.setUserName(m_connectionInfo.user);
m_serverDB.setPassword(m_connectionInfo.pass);
if ( !m_serverDB.open() ) {
sErrMsg = m_serverDB.lastError().text();
sErrMsg.prepend(SERVERDB_ERROR_MSG);
return bResult;
}
만약 이 때 에러 메시지로 "Driver not loaded"가 나오는 경우가 있는데 이때는 아래의 내용을 확인해봐야 합니다.
1. exe가 있는 폴더 혹은 library path에 아래의 dll이 있는지
- libcrypto-3-x64.dll
- libiconv-2.dll
- libintl-9.dll
- libpq.dll
- libssl-3-x64.dll
- libwinpthread-1.dll
여기서 중요한건 libwinpthread-1.dll 입니다. GPT나 Gemini한테 물어보면 libwinpthread-1.dll를 제외한 나머지 파일들의 유무를 많이 얘기하는데 PostgreSQL은 mingw/gcc로 빌드 하였고 만약 Qt build를 msvc로 했을 경우 문제가 발생할 수 있습니다.
2. exe가 있는 폴더에 sqldriver 폴더 생성 후 qsqlpsql.dll, qsqlodbc.dll 있는지
저도 이 문제 때문에 꽤 애를 먹었는데 [libwinpthread-1.dll] 파일을 exe에 추가하여 배포하였더니 사용자 PC에서도 문제가 없었습니다.
'Programming > Qt' 카테고리의 다른 글
| [Qt] QXlsx 사용 시 Cell Hyperlink 걸기 (0) | 2025.05.26 |
|---|---|
| [Qt] QTreewidget 메모리 해제 없이 비우기 (0) | 2024.11.27 |
| [Qt] QtConcurrent mappedReduced 사용하기 (0) | 2024.11.08 |
| [Qt] QTreeWiget Data Json 형태로 Save/Load 하는 방법 (0) | 2024.11.06 |
| [Qt] 다른 쓰레드에서 QTimer 처리하기 (1) | 2024.10.22 |




















