반응형

[주의] 

윈도우 환경에서 QT + MinGW를 이용하는 경우에는 MySQL 드라이버를 지정해도

에러가 발생합니다. (QSQLITE는 가능)

만약 MySQL 드라이버를 로드하려면 해당 소스를 재컴파일해서 사용해야 합니다.

 

아래 예제의 경우,

MySQL 드라이버를 재컴파일하지 않고 QMYSQL을 지정하면 드라이버 로딩 에러 메시지가 나오고,

QSQLITE를 지정하면 드라이버가 제대로 로딩되는 메시지가 나옵니다.

 

// reloading utf-8

// 소스에 보이는 한글이 QT 에디터에서 깨진 경우.. QT 메뉴의 Edit > Select Encoding에서 utf8을 선택하고

// Reload with Encoding 단추를 눌러보세요.  


#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
#include <QString>
#include <QLabel>

#include <QSql>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlError>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
    QString msg;
    QLabel *label = new QLabel;
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

    db.setHostName("localhost");
    db.setDatabaseName("testdb");

    bool ret = db.open();

    if( ret == false ) {
        msg = msg.fromLocal8Bit("<br><font color=blue> DB 드라이버 오픈에 실패했습니다.</font> <br><br>");
        label->setText(msg);
        label->show();
    } else {
        msg = msg.fromLocal8Bit("<br><font color=blue> DB 드라이버 오픈에 성공했습니다.</font> <br><br>");
        label->setText(msg);
                    // KOR());
        label->show();
        db.close();
    }

    return a.exec();
}

반응형

'Qt' 카테고리의 다른 글

Qt 공식 사이트  (0) 2022.05.25
QT 한글 출력 - 윈도우 cmd 콘솔 출력  (0) 2014.09.24

+ Recent posts