QT에서 Qt sql라이브러리가 아닌 mysql connector c++ 라이브러리를 사용할 수 있나요?

chobozzang의 이미지

안녕하세요.
qt5.0.2 버전을 깔았는데 mysql드라이버가 지원되지 않아서요... driver를 만드려고 하니 허접한 실력이라 계속 삽질만 하고 개발기간이 짧아서 mysql connector 라이브러리를 그냥 쓰려고 하는데 컴파일까지는 문제가 없는데 실행이 제대로 안되는 듯 해서요... 혹시 qtsql 라이브러리가 아닌 다른 db 라이브러리를 이용해 개발하신 분들 답변 좀 부탁드리겠습니다.

개발환경은 windows8, mingw 컴파일러를 사용하고 있습니다.

그리고 제가 라이브러리 링킹한 부분과 테스트소스에 문제점이 있으면 지적부탁드리겠습니다.
mysql connector 1.0.5 버전 위치는 C:\mysql

db.pro

unix {
    LIBS += -lmysqlcppconn
}
win32 {
    LIBS += "C:\mysql\lib\opt\mysqlcppconn-static.lib"
    LIBS += "C:\mysql\lib\opt\mysqlcppconn.lib"
    INCLUDEPATH += "C:\mysql\include"
}

테스트 소스

#include <QCoreApplication>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <QDebug>
 
using namespace sql::mysql;
 
int main(int argc, char *argv[])
{
    qDebug() << "start!!!";
    QCoreApplication a(argc, argv);
    qDebug() << "start";
 
    sql::Driver* driver_;
    sql::Connection* con_;
    sql::Statement* stmt_;
 
    try {
        driver_ = get_driver_instance();
 
        std::string url = "tcp://111.111.11.11:3306";
        std::string id = "abcd";
        std::string pass = "12345";
    con_ = driver_->connect(url, id, pass);
    stmt_ = con_->createStatement();
    std::string database("abcdef");
 
        bool retbal = stmt_->execute("USE " + database);
        //std::cout << retbal;
    }
    catch (sql::SQLException &e)
    {
        std::cout << " (MySQL error code: " << e.getErrorCode();
        std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;
    }
 
 
    delete stmt_;
    delete con_;
 
    return a.exec();
}

현재 qDebug() 로 찍은 것도 print가 안되네요 ㅠㅠ

klara의 이미지

실행하면 뭐라고 나오나요?

chobozzang의 이미지

저 상태로 실행하면 아무런 메시지가 없네요...ㅠㅠ 그 후 dependency 문제 때문인 것 같아서 mysql server 5.5 버전을 설치하고

unix {
LIBS += -llibmysql
LIBS += -lmysqlcppconn
}
win32 {
LIBS += "C:\mysql\lib\opt\mysqlcppconn-static.lib"
LIBS += "C:\mysql\lib\opt\mysqlcppconn.lib"
LIBS += "C:\mysqlserver\lib\libmysql.lib"
#LIBS += "C:\mysql\lib\opt\mysqlcppconn-static.lib"
INCLUDEPATH += "C:\mysql\include"
}

.pro 파일을 이렇게 수정한 후에 libmysql.dll 파일과 mysqlcppconn.dll 을 실행파일이 생기는 폴더에 복사해서 넣어 주었습니다.
그리고 소스 확인 결과 connect 를 호출할 때 죽는거 같은데요. 콘솔창에 2까지 찍히고 잠시 멈춰있다가 db.exe 파일이 중지되었습니다 라는 메시지와 함께 죽습니다.

#include <QCoreApplication>
//#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <QDebug>
 
using namespace sql;
 
int main(int argc, char *argv[])
{
    qDebug() << "start!!!";
    QCoreApplication a(argc, argv);
    qDebug() << "start";
 
    //sql::Driver* driver_;
    sql::Driver* driver_;
    sql::Connection* con_;
    sql::Statement* stmt_;
    sql::ResultSet* res;
 
    qDebug() << "1";
 
    try {
        driver_ = get_driver_instance();
        //driver_ = sql::get_driver_instance();
 
        std::string url = "tcp://1.2.3.4:3306";
        std::string id = "abcd";
        std::string pass = "abcde";
 
        qDebug() << "2";
 
        con_ = driver_->connect(url, id, pass);
 
        con_->setAutoCommit(0);
 
        std::string database("db");
        con_->setSchema(database);
 
        qDebug() << "3";
 
        stmt_ = con_->createStatement();
 
        qDebug() << "4";
 
        res = stmt_->executeQuery("SELECT * FROM result");
        //std::cout << retbal;
        qDebug() << "5";
 
        delete res;
        delete stmt_;
        con_->close();
        delete con_;
 
    }
    catch (sql::SQLException &e)
    {
        std::cout << " (MySQL error code: " << e.getErrorCode();
        std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;
    }
 
 
 
    return a.exec();
}

klara의 이미지

디버거는 못쓰시나요?
디버거를 못쓰신다면 모든 줄에 디버깅 메시지를 넣어보세요.
그리고 모든 포인터에 접근하기 전에 널이 아닌지 확인해보세요.

chobozzang의 이미지

connect 함수에서 죽었구요... 구글링 해보니 영어가 딸려서 제대로 해석한지는 모르겠지만 mysql 라이브러리 설치 시 소스로 mingw 컴파일러를 이용해 컴파일해서 설치해야 하는 것 같습니다. 과정이 더 복잡해질듯 해서 QT mysql 드라이버 설치해서 사용하기로 했습니다.
답변 감사합니다.

익명 사용자의 이미지

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.