linux c访问mysql数据库_Mysql —— linux下使用c语言访问mySql数据库

本文介绍了在Linux环境下,如何使用C语言连接并操作MySQL数据库。通过编译时指定头文件和库文件路径,利用mysql.h进行数据库交互。文中提供了创建数据库、创建表、初始化管理员账户的C代码示例,展示了查询数据库并打印结果的过程。
摘要由CSDN通过智能技术生成

在Linux编写完c代码运行出现没有找到mysql.h是因为没有指定头文件的位置和库文件的位置。

命令:gcc -I/usr/include/mysql XXXX.c -L/usr/lib/mysql -lmysqlclient -o XXXX

XXXX.c:你要编译的文件; XXXX:编译完的文件名;

(详解:需要在gcc编译时指定 头文件地址,用命令mysql_config,即可获取mysql安装后头文件所在位置,库文件所在位置,之后指定相关路径。-I /usr/include/mysql,同时需要指定mysql的库文件, 搜索目录为:-L /usr/lib64/mysql ,之后指定需要的库文件名:-lmysqlclient)

原文:https://blog.csdn.net/wb736/article/details/77193839

测试代码:(转载自https://blog.csdn.net/weixin_42167759/article/details/80848097)

/********************************************

* 编译命令:gcc aa.c -lmysqlclient -o aa

* 执行命令:./aa

* ******************************************/

#include

#include

#include

#include

#include "mysql/mysql.h"

MYSQL *g_conn;//mysql 链接

MYSQL_RES *g_res;//mysql 记录集

MYSQL_ROW g_row;//字符串数组,mysql 记录行

const char *g_host_name = "localhost";

const char *g_user_name = "root";

const char *g_password = "asdfgh";

const char *g_db_name = "test";

const unsigned int g_db_port = 3306;

#define MAX_BUF_SIZE 1024 //缓冲区最大字节数

char sql[MAX_BUF_SIZE];

char Time[MAX_BUF_SIZE];

int iNum_rows = 0;//mysql语句执行结果返回行数赋初值

int flag = 0;//管理员权限开关

int i = 1;//系统运行开关

/****************************************************

* time : 20180622

* addby : swj

* function :print_mysql_error() 打印错误信息

* ******************************************************/

void print_mysql_error(const char *msg)

{

if(msg)

printf("%s: %s\n",msg,mysql_error(g_conn));

else

puts(mysql_error(g_conn));

}

/****************************************************

* time : 20180622

* addby : swj

* function :executesql() 执行sql语句,成功返回0,失败返回-1

******************************************************/

int executesql(const char * sql)

{

if(mysql_real_query(g_conn,sql,strlen(sql)))

return -1;

return 0;

}

/****************************************************

* time : 20180622

* addby : swj

* function :init_mysql() 初始化链接

******************************************************/

int init_mysql()

{

//init the database connection

g_conn = mysql_init(NULL);

//connection the database

if(!mysql_real_connect(g_conn,g_host_name,g_user_name,g_password,g_db_name,g_db_port,NULL,0))

return -1;//链接失败

if(executesql("set names utf8"))

return -1;

return 0; //返回成功

}

/****************************************************

* * time : 20180622

* * addby : swj

* * function :create_database() 选择数据库 没有的时候 创建数据;有的时候 进去数据

* 库

* ******************************************************/

void create_database()

{

sprintf(sql,"use workdata");

if(executesql(sql) == -1)

{

puts("create database");

executesql("create database workdata;");

print_mysql_error(NULL);

puts("choice database");

executesql("use workdata;");

print_mysql_error(NULL);

puts("!!!Initialize the success!!!");

}

else

{

executesql("use workdata;");

print_mysql_error(NULL);

}

}

/****************************************************

* * time : 20180622

* * addby : swj

* * function :create_table() 创建表

* ******************************************************/

void create_table()

{

sprintf(sql,"show tables;");

executesql(sql);

g_res = mysql_store_result(g_conn);

iNum_rows = mysql_num_rows(g_res);

if(iNum_rows == 0)

{

puts("create users table");

executesql("create table users(id_ int(11) unsigned primary key auto_increment,name_ char(255) not null unique,password_ char(32) not null,create_time_ datetime,creator_id_ int(11) unsigned,auth_type_ int(11) not null,dyn_sn_ char(10),dyn_pass_sn_ text,remark_ varchar(200),foreign key(creator_id_) references users(id_));");

}

mysql_free_result(g_res);//释放结果集

}

/****************************************************

* * time : 20180622

* * addby : swj

* * function :init_administrator() 初始化管理员账户

* * 管理员用户名:root 密码:root

* * ******************************************************/

void init_administrator()

{

sprintf(sql,"select * from users where id_='1' and name_='root';");

executesql(sql);

g_res = mysql_store_result(g_conn);

iNum_rows = mysql_num_rows(g_res);

if(iNum_rows == 0)

{

puts("Init Administrtor User");

sprintf(sql,"insert into users values(1,'root','root','2017-08-18 12:21:11',1,0,'','','0:VIP 1:local pwd 2:local cert');");

executesql(sql);

}

mysql_free_result(g_res);//释放结果集

}

int main(void)

{

puts("!!!The system is initializing!!!");

/*初始化链接*/

//在初始化的时候 数据库 test 是必须事先创建好的 否则会报错Unknown database 'test'

if(init_mysql())

print_mysql_error(NULL);//当链接数据库时候 有错误 会报错

//选择数据库workdata 没有的时候 创建数据库 有的时候 进去数据库

create_database();

//创建表

create_table();

//初始化管理员账户

init_administrator();

//操作数据库 查询数据库 表中的信息

if(executesql("select * from users"))

print_mysql_error(NULL);

g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集

int iNum_rows = mysql_num_rows(g_res); // 得到记录的行数

int iNum_fields = mysql_num_fields(g_res); // 得到记录的列数

printf("共%d个记录,每个记录%d字段\n", iNum_rows, iNum_fields);

printf("id_ | name_ |password_| create_time_ |creator_id_ | auth_type_ |dyn_sn_| dyn_pass_sn_ |remark_ \n");

while ((g_row=mysql_fetch_row(g_res))) // 打印结果集

printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",g_row[0],g_row[1],g_row[2],g_row[3],g_row[4],g_row[5],g_row[6],g_row[7],g_row[8]); // 第一,第二字段

mysql_free_result(g_res); // 释放结果集

mysql_close(g_conn);

return EXIT_SUCCESS;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值