tags:

views:

173

answers:

2

I use Visual Studio 2008, MySQL 5.0.90,Qt 4.6.3 and C++ I try connect MySql but it gives this error messages.

  • Driver not loaded.

I am new programmer. How cann I solve this problem? Thank a lot.

A: 

(If you accept this answer, remember to click on the check mark to the left.)

You need to initialize the MySQL connection, something like this:

void
Manager ::
initialize_connection(const Dataset_Info& dsi)
{
    if (!m_connection_initialized)
    {
        //------------------------------------------------------------------
        //  For a fresh install of the database server, the menu_planner
        //      database won't exist (a database server, such as MySQL, may
        //      hold more than one database).  The application account may not
        //      exist either.
        //  However, once this application has an account, the database can
        //      be created:
        //      Conn = DriverManager.getConnection
        //            ("jdbc:mysql://localhost/?user=root&password=rootpassword");
        //      s=Conn.createStatement();
        //      int Result=s.executeUpdate("CREATE DATABASE databasename");
        //------------------------------------------------------------------
        wxMutexLocker   lock(m_driver_mutex);
        if (!m_connection_initialized)
        {
            m_p_sql_driver.reset(sql::mysql::get_mysql_driver_instance());
            std::string     host_name("tcp://127.0.0.1:3306/");
            host_name += dsi.m_dsn_name;
            try
            {
                m_p_db_connection.reset(m_p_sql_driver->connect(host_name.c_str(),
                                                         dsi.m_user_name,
                                                         dsi.m_password));
            }
            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 = "# ERR: SQLException in ";
                wx_text += __FILE__;
                wxLogDebug(wx_text);
                wx_text.Printf("# ERR: (%s) on line %d", __FUNCTION__, __LINE__);
                wxLogDebug(wx_text);
                wx_text.Printf("# ERR: %s (MySQL error code: %d, SQLState: %s)",
                               e.what(),
                               e.getErrorCode(),
                               e.getSQLState());
                wxLogDebug(wx_text);
                wxLogDebug("Verify that mysqlcppconn.dll is in the PATH or in the working directory.");
                throw Manager_Connection_Not_Initialized();
            }
            m_connection_initialized = true;
            m_schema_name = dsi.m_dsn_name;
        }
    }

    return;
}

Also verify that the MySQL driver is running as a service or daemon.

Don't forget, you need to grant permissions and your program as a user:

    //---------------------------------------------------------------------
    //  Permissions and Rights, for MySQL:
    //  The User needs to grant the following permissions and privileges
    //      to `mp_appl`@`localhost`:
    //          CREATE, DROP, EVENT, ALTER, DELETE, INDEX,
    //          INSERT, SELECT, UPDATE, CREATE TEMPORARY TABLES,
    //          LOCK TABLES, CREATE VIEW, SHOW VIEW, ALTER ROUTINE,
    //          CREATE ROUTINE, EXECUTE, FILE
    //  The GRANT commands (copy, then paste into MySQL command line client):
#if 0
    GRANT CREATE, DROP, EVENT, ALTER, DELETE, INDEX,
            INSERT, SELECT, UPDATE, CREATE TEMPORARY TABLES,
            LOCK TABLES, CREATE VIEW, SHOW VIEW, ALTER ROUTINE,
            CREATE ROUTINE, EXECUTE, FILE
        ON *.*
        TO 'mp_appl'@'localhost'
        IDENTIFIED BY 'menu';

    GRANT ALL PRIVILEGES
        ON menu_planner.*
        TO 'mp_appl'@'localhost'
        IDENTIFIED BY 'menu';

    //  To verify privileges:
    SHOW grants
        FOR 'mp_appl'@'localhost';

    //  To create user:
    CREATE
        USER 'mp_appl'@'localhost'
        IDENTIFIED BY 'menu';       
#endif
Thomas Matthews
A: 

The Qt installer does not provide MySQL driver. You'll need to compile Qt and MySQL module yourself. It might take a few hours. You can find a step-by-step guide at QtNode wiki: Qt4 on Windows.

EDIT: Problem has been discussed before:

Jacek