mysql_query分段错误

时间:2017-06-05 06:52:22

标签: mysql c

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <string.h>
#include <mysql.h>
#define IP 20
#define USER 20
#define PD 20
#define DB 20
//set configuration
struct configuration
{
    char* ip;
    int port;
    char* user;
    char* password;
    char* dbname;
};
struct configuration  getconf(const char*,const char*);

int main()
{
    //mysql
    struct configuration str_mysql;
    str_mysql=getconf("conf.txt","MYSQL");
    MYSQL *conn_mysql;
    MYSQL_RES *res_ptr;
    MYSQL_ROW sqlrow;
    conn_mysql=mysql_init(NULL);
    if(!conn_mysql)
    {
            printf("mysql_init failed\n");
            return EXIT_FAILURE;
    }
    //printf("%s %d %s %s %s\n",str_mysql.ip,str_mysql.port,str_mysql.user,str_mysql.password,str_mysql.dbname);
    conn_mysql = mysql_real_connect(conn_mysql,str_mysql.ip,str_mysql.user,str_mysql.password,str_mysql.dbname,3306,NULL,0);
    if(conn_mysql)
    {
            printf("Connection success\n");
    }
    else
    {
            printf("Connection failed\n");
            if(mysql_error(conn_mysql))
            {
                    printf("Connection error %d: %s\n",mysql_errno(conn_mysql),mysql_error(conn_mysql));
            }
    }
    printf("this doesn't work!!!");
    res=mysql_query(conn_mysql,"use leke_entiredb;");
    res = mysql_query(conn_mysql,"SELECT * from teacher;");
    printf("222");

我正在尝试使用函数mysql_query执行mysql查询,但是我得到了一个segementation fault.And func printf不起作用,直到我删除了func mysql_query.Any想法? 提示:我尝试使用mysql客户端选择数据,它没有问题。

0 个答案:

没有答案