Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  C语言  >  正文 C语言操作MySQL数据库,进行连接、插入、修改、删除

C语言操作MySQL数据库,进行连接、插入、修改、删除

发布时间:2018-10-08   编辑:www.jquerycn.cn
jquery中文网为您提供C语言操作MySQL数据库,进行连接、插入、修改、删除等资源,欢迎您收藏本站,我们将为您提供最新的C语言操作MySQL数据库,进行连接、插入、修改、删除资源
一个自己用的C语言操作MySQL数据库,进行连接、插入、修改、删除实现,下面分享给大家参考有需要的朋友可参考一下。

下面这段代码实现了连接到本地MySQL服务器上9tmd_bbs_utf8数据库,从数据表tbb_user中根据输入的userid取得该用户的用户名并打印输出到终端。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8187')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8187>

#if defined(_WIN32) || defined(_WIN64)  //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"  //我的机器上该文件在/usr/local/include/mysql下
 
//定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "select username from tbb_user where userid = %d"
 
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
    MYSQL mysql,*sock;    //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
    MYSQL_RES *res;       //查询结果集,结构类型
    MYSQL_FIELD *fd ;     //包含字段信息的结构
    MYSQL_ROW row ;       //存放一行查询结果的字符串数组
    char  qbuf[160];      //存放查询sql语句字符串
   
    if (argc != 2) {  //检查输入参数
        fprintf(stderr,"usage : mysql_select <userid>nn");
        exit(1);
    }
   
    mysql_init(&mysql);
    if (!(sock = mysql_real_connect(&mysql,"localhost","dbuser","dbpwd","9tmd_bbs_utf8",0,NULL,0))) {
        fprintf(stderr,"Couldn't connect to engine!n%snn",mysql_error(&mysql));
        perror("");
        exit(1);
    }
   
    sprintf(qbuf,SELECT_QUERY,atoi(argv[1]));
    if(mysql_query(sock,qbuf)) {
        fprintf(stderr,"Query failed (%s)n",mysql_error(sock));
        exit(1);
    }
   
    if (!(res=mysql_store_result(sock))) {
        fprintf(stderr,"Couldn't get result from %sn", mysql_error(sock));
        exit(1);
    }
   
    printf("number of fields returned: %dn",mysql_num_fields(res));
       
    while (row = mysql_fetch_row(res)) {
        printf("Ther userid #%d 's username is: %sn", atoi(argv[1]),(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0])) ;
        puts( "query ok !n" ) ;
    }
   
    mysql_free_result(res);
    mysql_close(sock);
    exit(0);
    return 0;  

//. 为了兼容大部分的编译器加入此行
}

编译的时候,使用下面的命令

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2266')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2266>

gcc -o mysql_select ./mysql_select.c -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient (-lz) (-lm)

后面两个选项可选,根据您的环境情况运行的时候,执行下面的命令

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5748')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5748>./mysql_select 1

将返回如下结果:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3895')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3895>

number of fields returned: 1
Ther userid #1 's username is: Michael
query ok !

您可能感兴趣的文章:
MySQL常见错误代码解析
mysql命令行操作大全 mysql命令行操作总结
php入门教程:php mysql数据保存,删除,修改,更新,查询 操作
mysql导入sql文件 mysql远程登录
linux下mysql添加用户、删除用户、授权、修改密码
python怎么连接mysql
ubuntu下mysql配置
mysql中操作数据表的一些命令
python如何连mysql数据库
python怎么连接数据库

[关闭]