2025年3月31日 星期一 乙巳(蛇)年 正月初一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > C语言

C编程:C语言连接mysql数据库

时间:10-10来源:作者:点击数:86

示例代码:

  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <mysql.h>
  • int main (int argc, char *argv[])
  • {
  • int res;
  • MYSQL_RES *result;
  • int numFields = 0;
  • int numRows = 0;
  • MYSQL_FIELD * field;
  • MYSQL_ROW row;
  • int i = 0;
  • MYSQL *conn;
  • // 步骤1: 初始化连接句柄
  • conn = mysql_init(NULL);
  • if (conn == NULL) { // 如果返回NULl说明初始化失败
  • printf("mysql_init failed!\n");
  • return EXIT_FAILURE;
  • }
  • // 步骤2:实际进行连接
  • // 参数分别为,conn连接句柄,host是MySQL所在主机或地址,user用户名,password密码,database_name数据库名,后面的都是默认
  • conn = mysql_real_connect(conn, "127.0.0.1", "root", "c123456", "", 3357, NULL, 0);
  • if (conn) { // 连接成功
  • printf("Connection success!\n");
  • } else {
  • printf("Connection failed!\n");
  • }
  • mysql_query(conn,"use ceshi"); // 切换数据库
  • mysql_query(conn,"create table t1 (id int primary key,name varchar(200),age int)"); // 创建 t1 表
  • mysql_query(conn,"insert into t1(id,name,age) values(1,'zhangsan',10)"); // 写入数据
  • mysql_query(conn,"insert into t1(id,name,age) values(2,'lisi',20)"); // 写入数据
  • mysql_query(conn,"insert into t1(id,name,age) values(3,'wangwu',30)"); // 写入数据
  • mysql_query(conn,"commit"); // 写入数据
  • res=mysql_query(conn,"select * from t1");
  • result=mysql_store_result(conn);
  • numFields = mysql_num_fields(result);
  • numRows = mysql_num_rows(result);
  • while(field = mysql_fetch_field(result))//返回结果集中的列信息(字段)
  • printf("%s\t", field->name);
  • printf("\n");
  • if(result)
  • {
  • while(row = mysql_fetch_row(result))//返回结果集中行的记录
  • {
  • for(i = 0; i < numFields; i++)
  • {
  • printf("%s\t", row[i]);
  • }
  • printf("\n");
  • }
  • }
  • mysql_free_result(result);
  • // printf("%s",result);
  • // 步骤3: 退出前关闭连接
  • mysql_close(conn);
  • return 0;
  • }

编译:gcc my.c -o main $(mysql_config --libs --cflags) -g

执行结果

  • [root@mysql ~]# ./main
  • Connection success!
  • id name age
  • 1 zhangsan 10
  • 2 lisi 20
  • 3 wangwu 30

对于show databases 之类命令显示

  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <mysql.h>
  • int main (int argc, char *argv[])
  • {
  • int res;
  • MYSQL_RES *result;
  • int numFields = 0;
  • int numRows = 0;
  • MYSQL_FIELD * field;
  • MYSQL_ROW row;
  • int i = 0;
  • MYSQL *conn;
  • // 步骤1: 初始化连接句柄
  • conn = mysql_init(NULL);
  • if (conn == NULL) { // 如果返回NULl说明初始化失败
  • printf("mysql_init failed!\n");
  • return EXIT_FAILURE;
  • }
  • // 步骤2:实际进行连接
  • // 参数分别为,conn连接句柄,host是MySQL所在主机或地址,user用户名,password密码,database_name数据库名,后面的都是默认
  • conn = mysql_real_connect(conn, "127.0.0.1", "root", "c123456", "", 3357, NULL, 0);
  • if (conn) { // 连接成功
  • printf("Connection success!\n");
  • } else {
  • printf("Connection failed!\n");
  • }
  • mysql_query(conn,"use ceshi"); // 切换数据库
  • mysql_query(conn,"create table t1 (id int primary key,name varchar(200),age int)"); // 创建 t1 表
  • mysql_query(conn,"insert into t1(id,name,age) values(1,'zhangsan',10)"); // 写入数据
  • mysql_query(conn,"insert into t1(id,name,age) values(2,'lisi',20)"); // 写入数据
  • mysql_query(conn,"insert into t1(id,name,age) values(3,'wangwu',30)"); // 写入数据
  • mysql_query(conn,"commit"); // 写入数据
  • res=mysql_query(conn,"show databases");
  • result=mysql_store_result(conn);
  • while(row = mysql_fetch_row(result))//返回结果集中行的记录
  • printf("%s\t", row[0]);
  • printf("\n");
  • mysql_free_result(result);
  • // printf("%s",result);
  • // 步骤3: 退出前关闭连接
  • mysql_close(conn);
  • return 0;
  • }
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门