C程序mysql连接 [英] C program mysql connection

查看:84
本文介绍了C程序mysql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个简单的c程序,该程序必须连接到我的数据库,然后执行查询,然后关闭连接.

I'm working on a simple c program that has to connect to my database, then do a query and then close the connection.

int main()
{
    MYSQL *conn;
    conn = mysql_init(NULL);

    if (conn == NULL) {
        printf("Error %u %s\n", mysql_errno(conn), mysql_error(conn));
        exit(1);
    }

    if (mysql_real_connect(conn, "localhost", "root", "root", NULL, 8889, NULL, 0)) {
        printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
        exit(1);
    }

    if (mysql_query(conn, "create database testdb")) {
        printf("Error %u: %s",mysql_errno(conn), mysql_error(conn));
        exit(1);
    }

    mysql_close(conn);
    return 0;
}

该代码可以编译,但是当我运行它时,它将在mysql_query()语句之后退出.

This code compiles but when I run it, it will exit after the mysql_query() statement.

返回以下错误:

Error 2006: MySQL server has gone away

我用google搜索了答案,并最终显示在这里:

I used google to search for an answer and ended up here:

http://dev.mysql.com/doc /refman/5.0/en/gone-away.html

推荐答案

返回值

如果连接成功,则为MYSQL *连接句柄;如果连接失败,则为NULL.对于成功的连接,返回值与第一个参数的值相同.

A MYSQL* connection handle if the connection was successful, NULL if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.

http://dev.mysql.com /doc/refman/5.0/en/mysql-real-connect.html

您没有建立连接

这篇关于C程序mysql连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆