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的更多相关文章
随机推荐
- 对象在hibernate中的状态
首先hibernate中对象的状态有三种:瞬态.游离态和持久态,三种状态转化的方法都是通过session来调用,瞬态到持久态的方法有save().saveOrUpdate().get().load() ...
- 【译文】InnoDB 的不同的SQL如何加锁
http://dev.mysql.com/doc/refman/5.6/en/innodb-locks-set.html 前置:检索如果用不到索引,会扫描全表,并根据策略加锁.所以,这就是我们合理建立 ...
- apache ActiveMQ之初体验
版权声明: https://blog.csdn.net/zdp072/article/details/27237549 一. 开篇语 继上一篇weblogic中使用jms发送和接受消息的文章后, 本文 ...
- 如何在Python中获取当前时间
所属网站分类: python基础 > 模块,库 作者:追梦骚年 链接:http://www.pythonheidong.com/blog/article/68/ 来源:python黑洞网,专注p ...
- shell基础--字符串和变量的操作
一.统计字符串长度 1.wc –L [root@~_~day4]# echo "hello" | wc -L 5 2.expr length string [root@~_~day ...
- Spring整合MyBatis(三)sqlSessionFactory创建
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.SqlSessionFactoryBean的初始化 二.获取 ...
- Windows App开发之应用布局与基本导航
简单演示样例看页面布局和导航 首先依照上一篇博客中的顺序来新建一个项目.新建好之后就点开MainPage.xaml開始写程序了. <Grid Background="{ThemeRes ...
- Spring的简单应用与基本原理
一:重要概念理解 Spring很简单,一定不要想得太复杂,只是有些东西很拗口而已 1:IOC(控制反转) 概念:利用反射的原理将对象创建的权利交给了Spring,Spring在运行的时候根据配置文件( ...
- 笔记:iOS随机数与随机数据集
//0 到 N - 1 之间的随机整数 NSUInteger r = arc4random_uniform(N); //1 到 N 之间的随机整数 NSUInteger r = arc4random_ ...
- C 修真之旅
前言 - 那久远的故事 工作好多年, 有时脑海里总回想儿时看的梦. 那时还刚上初中, 班上个子小的同学, 闲暇娱乐可能就是看 <飘渺之旅> 之类的小说. 前几年尝试满足自己少年时的记忆. ...