mysql_options()在mysqlclient.lib中崩溃

时间:2014-12-16 09:29:48

标签: mysql c mysql-connector

我正在尝试将MYSQL_PLUGIN_DIR与mysql_options()一起使用。在这样做时,我的应用程序崩溃了。

这是崩溃的简单代码 -

#include "stdafx.h"
#include <mysql.h>
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <Windows.h>
#include<process.h>

MYSQL *conn;        // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row;
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
    // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);

    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, NULL, 0, NULL, 0)) {
        printf("Conection error : %s\n", mysql_error(connection));
        exit(1);
    }
    return connection;
}

MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
    // send the query to the database
    if (mysql_query(connection, sql_query))
    {
        printf("MySQL query error : %s\n", mysql_error(connection));
        // exit(1);
    }

    return mysql_use_result(connection);
}
void mythread(void)
{


    mysql_thread_init();
    // assign the results return to the MYSQL_RES pointer
    res = mysql_perform_query(conn,"select 2");
    while ((row = mysql_fetch_row(res)) !=NULL)
        printf("%s\n", row[0]);

    /* clean up the database result set */
    mysql_free_result(res);
    /* clean up the database link */


}
void mythreadconnect(void)
{

    struct connection_details mysqlD;
    mysqlD.server = "localhost";  // where the mysql database is
    mysqlD.user = "root";       // the root user of mysql   
    mysqlD.password = "root"; // the password of the root user in mysql
    mysqlD.database = "myfirst";    // the databse to pick

    //connect to mysql
    conn = mysql_connection_setup(mysqlD);





}

int main()
{
    char path[500]="C:\\Users\\abhishek\\Documents\\Visual Studio 2010\\Projects\\sampleapplication\\Debug\\";
    mysql_library_init(0, NULL, NULL);
    mysql_init(conn);
    mysql_options(conn,MYSQL_PLUGIN_DIR ,path);
    mythreadconnect();
    mythread ();
    mysql_library_end();
    printf("Other business in Main\n"); 
    printf("Main is exiting\n");
    getch();
    return 0;
}

它崩溃在mysql_options(conn,MYSQL_PLUGIN_DIR,path);.我搜索了很多但找不到解决方案。请帮助解释此代码中的错误。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你的错误是mysql_init。使用此conn = mysql_init( NULL );。在您的情况下,您没有为conn分配内存并尝试初始化。阅读mysql_init param的文档并返回值。