linux_connect_mysql
原文来自 https://www.cnblogs.com/lywy510/p/3615710.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <mysql/mysql.h> MYSQL *g_conn;
MYSQL_RES *g_res;
MYSQL_ROW g_row; #define MAX_BUF_SIZE 1024 const char *g_host_name = "localhost";
const char *g_user_name = "root";
const char *g_password = "Ll123456..";
const char *g_db_name = "c_test";
const unsigned int g_db_port = ; void print_mysql_error(const char *msg) {
if (msg)
printf("%s:%s\n",msg,mysql_error(g_conn));
else
puts(mysql_error(g_conn));
} int executesql(const char *sql) {
if (mysql_real_query(g_conn,sql,strlen(sql)))
return -; return ;
} int init_mysql() {
g_conn = mysql_init(NULL);
if (!mysql_real_connect(g_conn,g_host_name,g_user_name,g_password,g_db_name,g_db_port,NULL,))
return -; return ;
} int main()
{
printf("hello world\n");
if (init_mysql())
print_mysql_error(NULL);
char sql[MAX_BUF_SIZE];
if (executesql(sql))
print_mysql_error(NULL);
if (executesql("select * from user;"))
print_mysql_error(NULL); g_res = mysql_store_result(g_conn);
int iNum_rows = mysql_num_rows(g_res);
int iNum_fields = mysql_num_fields(g_res);
printf("line = %d\t constent = %d\n", iNum_rows, iNum_fields);
puts("id\tname\n"); while ((g_row=mysql_fetch_row(g_res)))
printf("%s\t%s\n", g_row[], g_row[]); mysql_free_result(g_res);
mysql_close(g_conn);
return ;
}
编译命令

mysql 库文件自己找一下, 一般会使 mysql-dev.....
希望对你有用
linux_connect_mysql的更多相关文章
随机推荐
- Java 输入输出流总结
1. 运用BufferedInputStream 读取文件流和BufferedOutputStream写文件流: protected static void writeFile2(String inp ...
- Day2 CSS
什么是CSS 层叠样式表(cascading style sheet) 控制页面元素的显示方式.(添加样式) CSS语法 行间样式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出C ...
- 在Windows10中更改”WIN+E“快捷键打开目标
1> 复制下面代码到记事本保存为launch.vbs 2> 然后打开Regedit.exe并创建以下注册表分支 HKCU:\Software\Classes\CLSID\{52205fd8 ...
- saltstack之混合匹配
需要-C参数: salt -C ## 使用grains属性来匹配 [root@hadoop0 pillar]# salt -C 'G@os:Ubuntu' test.ping uadoop1: Tru ...
- 论mysql主从复制里面的那些坑
1.找好配置文件,修改对的配置文件,有的时候会有多个配置文件,要搞清楚加载的哪个配置文件. 2.主库备份钱的操作除了设置 只读状态外,还要设置全局只读=1. 3.mysqldump备份sql很方便,恢 ...
- CUDA和OpenGL互操作经典博文赏析和学习
1.使用cuda+opengl图形互操作性实现MPR.原学位论文学习:实时交互的医学图像可视化.在该论文的第5.1.1节. 2.cuda与opengl互操作之PBO 3.cuda与opengl互操作之 ...
- linux 修改时间永久
date -s "20190421 16:36:20" &&hwclock --systohc
- iOS下微信语音播放之切换听筒和扬声器的方法解决方案
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO,这个功能是开启红外感应 // ...
- linux文件系统初始化过程(5)---加载initrd(下)
一.目的 linux把文件分为常规文件.目录文件.软链接文件.硬链接文件.特殊文件(设备文件.管道文件.socket文件等)几种类型,分别对应不同的新建函数sys_open().sys_mkdir() ...
- 浅谈User Information List
[User Information List]用于查看一个site collection所有可以访问的用户信息.一个site collection只有一个User Information List表. ...