MySQL C API 访问 MySQL 示例
代码:

/* Simple C program that connects to MySQL Database server */
#include <mysql.h>
#include <stdio.h>
main() {
char *begin="\n+--------------BEGIN---------------+\n\n";
printf(begin);
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "your mysql user";
char *password = "your password";
char *database = "your database";
conn = mysql_init(NULL); /* Connect to database */
/*
* CLIENT_MULTI_RESULTS
* 通知服务器,客户端能够处理来自多语句执行或存储程序的多个结果集。
* 如果设置了CLIENT_MULTI_STATEMENTS,将自动设置它。
*/
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, CLIENT_MULTI_RESULTS)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
char *tell="SQL Table Query...\n";
printf(tell);
// SQL 普通表查询
char *sql="select password from Users whereUserName='client1@192.168.1.122'";
if (mysql_query(conn, sql)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("SqlCommand:%s",sql);
printf("\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("PassWord:%s \n\n", row[0]);
}
mysql_free_result(res);
char *tell2="SQL Store Query More...\n";
printf(tell2);
// SQL 存储过程查询
char *sql1="call P_GetCurrentCostPriceByUserName('client1@192.168.1.122')";
if (mysql_query(conn, sql1)) {
fprintf(stderr, "%s\n\n", mysql_error(conn));
exit(1);
}
/*
* 存储过程默认返回的是多个结果集,
* 所以要用mysql_next_result取出并检查
* 下一个的结果集。否则在存储过程下的
* 其它查询语句都会出现 “Commands out of sync;
* you can't run this command now”错误!
*/
do {
if ((res = mysql_use_result(conn))) {
printf("SqlCommand:%s",sql1);
printf("\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("UserName:%s \n", row[0]);
printf("Balance:%s \n",row[1]);
printf("Price:%s \n\n",row[2]);
}
}
}while (!mysql_next_result(conn));
mysql_free_result(res);
char *tell3="SQL View Query More...\n";
printf(tell3);
// SQL 视图查询
char *sql2="select CameraID,URL,RtspName,PW,PTZ,PTZServer from V_UserEquipment whereLoginName='client1@192.168.1.122'";
if (mysql_query(conn, sql2)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("SqlCommand:%s",sql2);
printf("\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("CameraID:%s \n", row[0]);
printf("URL:%s\n",row[1]);
printf("RtspName:%s \n",row[2]);
printf("PW:%s \n", row[3]);
printf("PTZ:%s \n",row[4]);
printf("PTZServer:%s \n\n",row[5]);
}
mysql_free_result(res);
mysql_close(conn);
char *end="+--------------END----------------+\n";
printf(end);
}

编译:
gcc -o sqla $(mysql_config --cflags) sqla.c $(mysql_config --libs)
运行结果:

+--------------BEGIN---------------+
SQL Table Query...
SqlCommand:select password from Users where UserName='client1@192.168.1.122'
PassWord:client1
SQL Store Query More...
SqlCommand:call P_GetCurrentCostPriceByUserName('client1@192.168.1.122')
UserName:client1@192.168.1.122
Balance:30000
Price:0.05
SQL View Query More...
SqlCommand:select CameraID,URL,RtspName,PW,PTZ,PTZServer from V_UserEquipment where LoginName='client1@192.168.1.122'
CameraID:051010049@192.168.1.122_0
[url=rtsp://192.168.1.93/1.mp4]URL:rtsp://192.168.1.93/1.mp4[/url]
RtspName:admin
PW:admin
PTZ:1
PTZServer:ptzserver1@192.168.1.122
+--------------END----------------+

MySQL C API 访问 MySQL 示例的更多相关文章
- Linux下eclipse及mysql安装,c++访问mysql数据库
这两天在学习linux下用c++访问mysql,碰到一堆问题,记录一下. 1.mysql安装: 公司的电脑是64位的,安装的是64为的RHEL4,安装如下三个包: MySQL-client-5.1.4 ...
- mysql C API的使用
<MySQL++简介>介绍了如何使用C++来访问mysql,本文记录下使用C API访问mysql,mysql++就是对本文介绍的C-API的封装. 常用函数(名字就能告诉我们用法): M ...
- PHP16 PHP访问MySQL
学习要点 PHP访问MySQL配置 PHP访问MySQL函数介绍 足球赛程信息管理 PHP访问MySQL配置 PHP.ini配置文件确认以下配置已经打开 extension=php_mysql.dll ...
- Postman如何通过xmysql工具的Restful API 接口访问MySQL
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 导语 有时候用 Postman 接口测试需要获取MySQL的查询结果做接口输出的校验,这里介绍下 Postman 通过 R ...
- MySQL C API概述
以下列表总结了C API中可用的功能.有关更多详细信息,请参见 第27.8.7节“C API函数描述”中的说明. my_init():在线程安全程序中初始化全局变量和线程处理程序 mysql_affe ...
- C Mysql API连接Mysql
最近都在查看MYsql C API文档,也遇到了很多问题,下面来简单的做一个总结. mysql多线程问题 mysql多线程处理不好,经常会发生coredump,见使用Mysql出core一文. 单线程 ...
- MySql接口API函数综述
C API函数概述 函数 描述 mysql_affected_rows() 返回上次UPDATE.DELETE或INSERT查询更改/删除/插入的行数. mysql_autocommit() 切换 a ...
- Linux下C++访问MySQL数据库
由于想要开始了解并学习用LAMP进行web开发,所以昨晚我在Fedora上安装了MySQL,学习了MySQL的几个常用命令.想着在学习进行web开发(PHP访问数据库)之前,先用我熟悉的C++连接数据 ...
- 开源一个好用的nodejs访问mysql类库
一.背景问题 自nodejs诞生以来出现了一大批的web框架如express koa2 egg等等,前端可以不再依赖后端可以自己控制服务端的逻辑.原来的后端开发同学的阵地前端如今同样也写的风生水起,撸 ...
随机推荐
- Qt编写自定义控件12-进度仪表盘
前言 进度仪表盘主要应用场景是标识一个任务进度完成的状况等,可以自由的设置范围值和当前值,为了美观还提供了四种指示器(圆形指示器/指针指示器/圆角指针指示器/三角形指示器),各种颜色都可以设置,其中的 ...
- Spark2.1.0模型设计与基本架构(上)
随着近十年互联网的迅猛发展,越来越多的人融入了互联网——利用搜索引擎查询词条或问题:社交圈子从现实搬到了Facebook.Twitter.微信等社交平台上:女孩子们现在少了逛街,多了在各大电商平台上的 ...
- 基于GoogLeNet的不同花分类微调训练案例
import tensorflow as tf from tensorflow.contrib.slim import nets slim = tf.contrib.slim import numpy ...
- [转]本地 Windows 计算机密码登录 登录 腾讯云 Linux 实例
本文转自:https://cloud.tencent.com/document/product/213/5436? 登录工具 使用 远程登录软件 ,采用密码登录 Linux 实例(本例中选择使用 Pu ...
- MFC函数—CSingleDocTemplate
前提:在InitInstance() 函数的初始化过程中,我们可以看到代码CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDoc ...
- [日常] Linux下的docker实践
1.Linux 发展出了另一种虚拟化技术:Linux 容器(Linux Containers,缩写为 LXC) 2.Linux 容器不是模拟一个完整的操作系统,而是对进程进行隔离 3.Docker 属 ...
- Windows Server 2008R2常见的500错误
每次公司服务器装系统后再去部署服务,都会碰到这个问题,这里记录一下问题的解决方法 遇到“500 – 内部服务器错误. 您查找的资源存在问题,因而无法显示.”的问题. 解决办法: 1.解决方法:打开II ...
- django rest_framework Serializers 序列化组件
为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成json格式,那就需要对我们从数据库拿 ...
- Bash:字符串操作
参考:http://blog.csdn.net/finewings/article/details/5718133 字符串提取 去掉指定前缀 1. ${varible#pattern} ...
- Code Signal_练习题_alphabeticShift
Given a string, replace each its character by the next one in the English alphabet (z would be repla ...