Windows/MFC

mysql connect

aucd29 2013. 10. 2. 18:18
별건 없긴 하지만 -_ -암튼... MFC8.0 + MYSQL 을 해봣다.. PHP 에서 썻던지라 함수명도 반갑기도 하고.. 클래스나 만들어야 겠구나...

연결을 위해서는 달리 크게 할건 없지만.. 다음에 과정을 거쳐야 한다.

1. mysql.com 사이트에서 src 를 받아서 우선 컴파일 한다. 그럼 dbug 폴더 내에 release_obj 에 lib 파일과 dll 파일이 생성이 되는데 그걸 참조해야한다.... 간단하게 lib 라는 폴더 하나 만들어 두고 넣어두자 그리곤 config 란에 debugging 쪽에 working directory 가 존재하는데 이곳에 lib 폴더를 지정한다.


2. mysql src 내에 include 폴더에 위치를 지정해주던지 프로젝트의 내에 복사를 해둔다. config 란에 C/C++ 쪽 부분에 additional include directories 에 include path를 지정해준다.

ps. 컴파일하기 귀찮다면 게시물 밑에서 파일을 다운 받아라....

BOOL Cmysql_testDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog. The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    USES_CONVERSION;

    m_listBasic.SetExtendedStyle(m_listBasic.GetExtendedStyle() | LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
    m_listBasic.InsertColumn(0, L"a", LVCFMT_CENTER, 30);
    m_listBasic.InsertColumn(1, L"b", LVCFMT_CENTER, 30);
    m_listBasic.InsertColumn(2, L"c", LVCFMT_LEFT, 350);

    MYSQL mysql;
    mysql_init(&mysql);

    if (!mysql_real_connect(&mysql, _HOST, _USER, _PASS, _DB, 3306, 0, 0))
    {
        AfxMessageBox(A2CT(mysql_error(&mysql)));
        return false;
    }
    mysql_select_db(&mysql, _DB);
    int res = mysql_query(&mysql, "SELECT * FROM basic");
    if (res != 0)
    {
        AfxMessageBox(A2CT(mysql_error(&mysql)));
        return false;
    }

    MYSQL_RES * result;
    MYSQL_ROW sql_row;

    CString szRes;
    result = mysql_store_result(&mysql);

    wchar_t szTitle[255]={0};
    int i = 0;
    while((sql_row=mysql_fetch_row(result))!=NULL)
    {
        memset(szTitle, 0, sizeof(wchar_t) * 255);
        MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)sql_row[2], strlen(sql_row[2]), szTitle, 255);

        m_listBasic.InsertItem(i, A2CT(sql_row[0]), 0);
        m_listBasic.SetItemText(i, 1, A2CT(sql_row[1]));
        m_listBasic.SetItemText(i, 2, szTitle);
        ++i;
    }
    mysql_free_result(result);
    mysql_close(&mysql);

    return TRUE; // return TRUE unless you set the focus to a control
}