c语言访问MySQL数据库

1、 以用户名root和密码yxk的mysql数据库来连接本机服务器上名为test的数据库(test必须为mysql数据库中的一个存在的数据库):

//connect1.c

#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"

int main()
{
    MYSQL *conn_ptr;
    conn_ptr = mysql_init(NULL);
    if(!conn_ptr)
    {
        fprintf(stderr,"mysql_init failed\n");
    return EXIT_FAILURE;
    }
    conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","yxk","test",0,NULL,0);
    if(conn_ptr)
    {
        printf("Connection success\n");
    }
    else
    {
        printf("Connection failed\n");
    }
    mysql_close(conn_ptr);
    return EXIT_SUCCESS;
}

编译命令为 $gcc –I /usr/include/mysql connect1.c –lmysqlclient –o connect1

执行: $ ./connect1

       Connection success

       $

注:如找不到mysql.h则查看《ubuntu12.04中找不到mysql.h》博文。

2、反馈链接失败错误:用户名root和密码yxk的mysql数据库来连接本机服务器上名为test的数据库,密码空着

//connect2.c

#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc,char *argv[])
{
    MYSQL my_connection;
    mysql_init(&my_connection);
    if(mysql_real_connect(&my_connection,"localhost","root"," ","test",0,NULL,0))
    {
        printf("Connection success\n");
        mysql_close(&mysql_connection);
    }
    else
    {
        printf("Connect failed\n");
        if(mysql_errno(&my_connection))
        {
            fprintf(stderr,"Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
        }
    }
    return EXIT_SUCCESS;
}

执行:

$ ./connect2

Connection filed

Connection error 1045: Access denied foruser: ‘neusoft@localhost’ (Using password: YES)

$

3、插入数据操作(用户名root和密码yxk的mysql数据库来连接本机服务器上名为test的数据库):

//insertdata.c

#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"

int main(int argc,char *argv[])
{
    MYSQL my_connection;
    int res;
    mysql_init(&my_connection);
    if(mysql_real_connect(&my_connection,"localhost","root","yxk","test",0,NULL,0))
    {
        printf("Connection success\n");
        res = mysql_query(&my_connection,"INSERT INTO children(fname,age) VALUES('Ann',13)");
        if(!res)
       {
           printf("Insert %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
        else
        {
            fprintf(stderr,"Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
        mysql_close(&my_connection);
    else
    {
        fprintf(stderr,"Connection failed\n");
        if(mysql_errno(&my_conncetion))
        {
            fprintf(stderr,"Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
        }
    }
    return EXIT_SUCCESS;
}  

编译命令为 $gcc –I /usr/include/mysql insertdata.c –lmysqlclient –o insertdata

执行: $ ./insertdata

   Connect success

   Insert 1 rows;


4、查找插入数据内容(用户名root和密码yxk的mysql数据库来连接本机服务器上名为test的数据库):

//insertdata.c

#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"

int main(int argc,char *argv[])
{
    MYSQL my_connection;
    MYSQL_RES *res_ptr;
    MYSQL_ROW sqlrow;
    int res;
    mysql_init(&my_connection);
    if(mysql_real_connect(&my_connection,"localhost","root","yxk","test",0,NULL,0))
    {
        printf("Connection success\n");
        res = mysql_quenry(&my_connection,"INSERT INTO children(fname,age) VALUES('Robert',8)");
        if(!res)
        {
            printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
        }
        else
        {
            fprintf(stderr,"Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
        }
        res = mysql_query(&my_connection,"SELECT LAST_INSERT)ID()");
        if(res)
        {
            printf("SELECT error:%s\n",mysql_error(&my_connection));
        }
        else
        {
            res_ptr = mysql_use_result(&my_connection);
            if(res_ptr)
            {
                 while(sqlrow = mysql_fetch_row(res_ptr)))
                {
                    printf("We inserted childNo %s\n",sqlrow[0]);
                }
                mysql_free_result(res_ptr);
            }
        }
        mysql_close(&my_connection);
    }
    else
    {
        fprintf(stderr,"Connection failed\n");
        if(mysql_errno(&my_connection))
        {
            fprintf(stderr,"Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
        }
    }
    return EXIT_SUCCESS;
}        
编译:

$ gcc –I/usr/include/mysql insertdate.c –lmysqlclient –o insertdate
$ ./insertdate
Connection success
Inserted 1 rows
We inserted childNo 3

$./insertdate
Connection success 
Inserted 1 rows
We inserted childNo 4

5、提取数据用户名root和密码yxk的mysql数据库来连接本机服务器上名为test的数据库

//selectdata.c

#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"

MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;

int main(int argc,char *argv[])
{
    int res;
    mysql_init(&my_connection);
    if(mysql_real_connect(&my_connection,"localhost","root","yxk","test",0,NULL,0));
    printf("Connection success\n");
    res = mysql_query(&my_connection,"SELECT chileNo,fname,age FROM children WHERE age > 5");
    if(res)
    {
        printf("SELECT error:%s\n",mysql_error(&my_connection));
    }
    else
    {
        res_ptr = mysql_store_result(&my_connection);
        if(res_ptr)
        {
            printf("Retrived %lu rows\n",(unsigned long)mysql_num_rows(res_ptr));
            while((sqlrow = mysql_fetch_row(res_ptr)))
            {
                printf("Fetched data...\n");
             }
            if(mysql_errno(&my_connection))
            {
                fprintf(stderr,"Retrive error:%s\n",mysql_error(&my_connection));
             }
        }
        mysql_free_result(res_ptr);
      }
    mysql_close(&my_connection);
    }
    else
    {
        fprintf(stderr,"Connection failed\n");
        if(mysql_errno(&my_connection))
        {
            fprintf(stderr,"Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
         }
     }
      return EXIT_SUCCESS;
}






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值