C语言是我比较喜欢的语言,可惜毕业后从事SQL这方面的工作了,虽然SQL也是俺喜欢的,不过SQL和后来的DATABASE没有带来足够的安全感和富裕的收入.混得狗一样!
1 下载MYSQL,C语言包
https://downloads.mysql.com/archives/c-c/
tar -zxvf mysql-connector-c-6.1.11-linux-glibc2.12-x86_64.tar
复制
解压后进入该目录把INCLUDE MV 项目下的INCLUDE目录 重命名为MYSQL_CONNECT
把LIB东西全部复制到 项目下的LIB
2 项目目录
[shark@OS7-GCC-MYSQL8-NETBEAN LinckMysql]$ ll
总用量 8
drwxrwxr-x. 2 shark shark 6 12月 4 21:49 bin
drwxrwxr-x. 2 shark shark 6 12月 4 21:49 conf
drwxrwxr-x. 3 shark shark 26 12月 11 17:08 include
drwxrwxr-x. 2 shark shark 4096 12月 11 17:10 lib
-rw-rw-r--. 1 shark shark 1395 12月 11 17:39 Main.c
复制
3 创建数据库用户
create user 'shark'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
grant all on shark to 'shark'@'%';
UPDATE mysql.user SET select_priv='Y' , insert_priv='Y', update_priv='Y', DELETE_priv='Y' WHERE HOST='%' and user='shark';
FLUSH PRIVILEGES;复制
4 编译小知识
#编译
gcc -g Main.c -I include -lmysqlclient -L lib -o test.exe
#下面是编译参数
-g:带调试信息
-I: 头文件
-l:小写的L,动态库名
-L: 动态库目录和静态库
-O: 输出可执行文件
复制
5 源码示列
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define HOST "192.168.2.30"
#define USERNAME "shark"
#define PASSWD "123456"
#define DATABASE "shark"
#include "mysql_connect/mysql.h"
int main(int argc, char const *argv[])
{
MYSQL my_connection;
int res; //SQL执行返回标志
char sql[]={"select * from books"};
MYSQL_RES* res_ptr; //返回结果集指针
MYSQL_ROW result_row; //返回行
int row,column; //行列
printf("%s\n",sql);
mysql_init(&my_connection); //初始化链接
if(mysql_real_connect(&my_connection,HOST,USERNAME,PASSWD,DATABASE,3306,NULL,CLIENT_FOUND_ROWS))
{
printf("DataBase connection succ!\n");
mysql_query(&my_connection,"set names utf8mb4"); //设置字符集
res =mysql_query(&my_connection,sql); //执行查询语句
printf("affected:%d Rows \n",mysql_affected_rows(&my_connection)); //获取影响行数
if(res)
{
printf("Error:mysql_query !\n");
printf("Faile %s\n",mysql_error(&my_connection)); //返回错误
mysql_close(&my_connection); //关闭链接
}
else
{
res_ptr = mysql_store_result(&my_connection); //获取这次结果集
if(res_ptr) //结果集不为空
{
row=mysql_num_rows(res_ptr); //获取行数
column=mysql_num_fields(res_ptr); //获取列数
MYSQL_FIELD* FIELD_NAME_ARRY=mysql_fetch_fields(res_ptr); //获取列名
for (int c=0; c < column; c++)
{
printf("%s\t",FIELD_NAME_ARRY[c].name);
}
printf("\n");
for(int i=1; i< row+1;i++) //打印行内容
{
result_row= mysql_fetch_row(res_ptr); //提取行到行变量
for (int j=0; j < column;j++)
{
printf("%s\t",result_row[j]); //从行变量 获取列的VALUE
}
printf("\n");
}
}
mysql_close(&my_connection);
}
}
else
printf("database connect fail! %s",mysql_error(&my_connection));
}复制
mysql_xxxx 就是MYSQL提供的函数
#include "mysql_connect/mysql.h"
复制
这个就是要调用的头文件,写法自由,前面没有 include 那么编译的时候要指定
这个include 是指双引号里的"include/mysql_connect/mysql.h"
#define HOST "192.168.2.30"
#define USERNAME "shark"
#define PASSWD "123456"
#define DATABASE "shark"
这段 采用此方式定义数据库连接信息
MYSQL my_connection;
这个就是 链接对象
6 运行结果
[shark@OS7-GCC-MYSQL8-NETBEAN LinckMysql]$ gcc -g Main.c -I include -lmysqlclient -L lib -o test.exe
[shark@OS7-GCC-MYSQL8-NETBEAN LinckMysql]$ ./test.exe
select * from books
DataBase connection succ!
affected:-1 Rows
id title price publishDate
3 Java编程思想 98.50 2005-01-02 00:00:00
4 HeadFirst设计模式 55.70 2010-11-09 00:00:00
5 第一行Android代码 69.50 2015-06-23 00:00:00
6 第一行Android代码 69.50 2015-06-23 00:00:00
7 C++编程思想 88.50 2004-01-09 00:00:00
8 HeadFirst Java 55.70 2013-12-17 00:00:00
9 疯狂Android 19.56 2014-07-31 00:00:00
复制
结果集不太美观,没有MYSQL网格线
7 MYSQL提供C语言函数说明
https://dev.mysql.com/doc/c-api/8.0/en/
8 C语言动态链接库设置
环境变量:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib
还可以使用Idconfig 配置/ect/ld.so.conf.d/,并对ldconfig进行更新。
可以先通过/etc/ld.so.conf.d/路径下查找已经链接的库,然后向其中添加conf文件
touch bit.conf
然后将库的路径写入到conf文件中。并进行库的路径更新:ldconfig
其他C语言文章