原文来自 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的更多相关文章

随机推荐

  1. UE4的csv文件导入、URL地址的读取及动态材质的设置

    1.csv文件的导入 UE4是可以直接导入csv文件的,其过程和其他文件资源(图片Texture,静态网格物体StaticMesh等)相似,但在导入过程中有一些需要注意的点. 如下图所示 这是一份编辑 ...

  2. Extjs TreePanel API详解

    转自:http://web.qhwins.com/CSS-JS-XML/2011091312092944999107.html config定义{ animate : Boolean, contain ...

  3. virtualbox+vagrant学习-4-Vagrantfile-2-Configuration Version

    Configuration Version 配置版本是vagrant 1.1+能够与vagrant 1.0保持向后兼容的机制.同时引入了引人注目的新特性和配置选项. 如果你运行了vagrant ini ...

  4. PAT乙级1001

    https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528 #include<bits/stdc+ ...

  5. 《Python核心编程》第二版第五章答案

    本人python新手,答案自己做的,如果有问题,欢迎大家评论和讨论! 更新会在本随笔中直接更新. 5-1.整型.讲讲Python普通整型和长整型的区别. Python的标准整形类型是最通用的数字类型. ...

  6. 【图像处理】Schmid滤波器

    Schmid也是一种类Gabor图像滤波器,在这篇文章[1]中有详细推导和介绍. 一种更简洁的表达公式是: 当中,r为核半径,Z为归一化參数,τ和σ是比較重要的參数,在ReID提取TextFeatur ...

  7. 关于Android

    1:Handle与多线程 Handle是什么?官方说明:handle是Android给我们提供用来更新UI的一套机制,也是一套消息处理的机制.可以看出handle主要就是两个功能,一个是更新UI,另一 ...

  8. java.sql.SQLSyntaxErrorException: ORA-01722: 无效数字

    ### Error updating database. Cause: java.sql.SQLSyntaxErrorException: ORA-01722: 无效数字 ### The error ...

  9. 【js】走近小程序(2) 常见问题总结

    一.API请求? 二.基础库兼容? 三.不同页面之间的传值   一.API请求? wx.request({ url: 'test.php', // 仅为示例,并非真实的接口地址 data: { x: ...

  10. 在windows上安装不同(两个)版本的Mysql数据库

    1.起因: 需要导入一个sql文件,发现死活导不进去.当执行到这一句时,就有问题.经过一番搜索,原来是我的数据库版本(原先Mysql版本5.5)低了,而支持该语句的版本应该是至少要5.7.那我索性就去 ...