簡體   English   中英

無法使用mysql ++連接到SQL數據庫

[英]Can't connect to SQL database with mysql++

我一直在嘗試在應用程序(基於Windows x64的應用程序)中使用mysql ++庫,但似乎無法連接到我的sql服務器。 一些信息:

我使用以下代碼連接到服務器:

mysqlpp::Connection conn(db, 0, user, pass, 3306);

這肯定有正確的數據。

然后,我的sql server是MySQL安裝中的標准服務。 而且我很確定我使用了標准設置。 我可以使用MySql Workbench連接到它,並編輯了一些新表,但是我自己的程序似乎沒有連接。

我閱讀了文檔,但找不到任何可能暗示我無法連接的具體信息。

哦,這么多問題,這么少的時間...

您是否檢查過程序是否有權訪問數據庫?

您的程序是否具有正確的特權?

您的主機名正確嗎?

您遇到什么錯誤?

拋出什么異常?

使用調試器時,錯誤在哪一行?

這是我的方法:

sql::Connection * const
Manager ::
get_db_connection(void) const
{
    //-------------------------------------------------------------------------
    //  Use only one connection until proven that more connections will make
    //      the program more efficient or have a beneficial impact on the user.
    //  Thus the change in returning sql::Connection * rather than a smart pointer.
    //      A smart pointer will delete its contents.
    //-------------------------------------------------------------------------
    static const char                   host_text[] = "tcp://127.0.0.1:3306/";
    static std::string                  host_name;
    if (!m_connection_initialized)
    {
        host_name = host_text;
        initialize_db_driver();
        host_name += m_dataset_info.m_dsn_name;
        try
        {
            m_p_connection = m_p_sql_driver->connect(host_name.c_str(),
                                                   m_dataset_info.m_user_name.c_str(),
                                                   m_dataset_info.m_password.c_str());
        }
        catch (sql::SQLException &e)
        {
            /*
            The MySQL Connector/C++ throws three different exceptions:

            - sql::MethodNotImplementedException (derived from sql::SQLException)
            - sql::InvalidArgumentException (derived from sql::SQLException)
            - sql::SQLException (derived from std::runtime_error)
            */
            wxString    wx_text = wxT("# ERR: SQLException in ");
            wx_text += wxT(__FILE__);
            wxLogDebug(wx_text);
            wx_text.Printf(wxT("# ERR: (%s) on line %d"),
                           __FUNCTION__,
                           __LINE__);
            wxLogDebug(wx_text);
            wx_text.Printf(wxT("# ERR: %s (MySQL error code: %d, SQLState: %s)"),
                e.what(),
                e.getErrorCode(),
                e.getSQLState());
            wxLogDebug(wx_text);
            wxLogDebug(wxT("Verify that mysqlcppconn.dll is in the PATH or in the working directory."));
            //      throw Manager_Connection_Not_Initialized();
            m_connection_initialized = false;
        }
        catch (...)
        {
            std::cout << "Unhandled database SQL exception\n" << flush;
            m_connection_initialized = false;
        }           
        m_connection_initialized = true;
    }
    return m_p_connection;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM