linux c调用 mysql代码
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <time.h>
using namespace std; MYSQL *g_conn; // mysql 连接
MYSQL_RES *g_res; // mysql 记录集
MYSQL_ROW g_row; // 字符串数组,mysql 记录行 #define MAX_BUF_SIZE 1024 // 缓冲区最大字节数 const char *g_host_name = "localhost";
const char *g_user_name = "root";
const char *g_password = "python123";
const char *g_db_name = "db_data_20170524164100";
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)
{
/*query the database according the sql*/
if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
return -; // 表示失败 return ; // 成功执行
} int init_mysql() // 初始化连接
{
// init the database connection
g_conn = mysql_init(NULL); /* connect the database */
if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, )) // 如果失败
return -; // 是否连接已经可用
executesql("set names utf8"); // 如果失败
// return -1;
return ; // 返回成功
} int main(void)
{
if (init_mysql());
print_mysql_error(NULL); char sql[MAX_BUF_SIZE]; int firstTime = time((time_t*)NULL);
printf("firstTime is: %d\n", firstTime); if (executesql("select * from data_stats_gov_cn_diqu_niandu")) // 句末没有分号
print_mysql_error(NULL); g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 int iNum_rows = mysql_num_rows(g_res); // 得到记录的行数
int iNum_fields = mysql_num_fields(g_res); // 得到记录的列数 printf("共%d个记录,每个记录%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[0], g_row[1]); // 第一,第二字段
{
for(int i=; i<iNum_fields; i++)
{
printf("%s\t",g_row[i]);
}
printf("\n");
} int secodnT
方法二:
这段代码链接:https://andrew913.iteye.com/blog/433280
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql/mysql.h>
#define MAX_COLUMN_LEN 32
int main(int argc , char *argv[])
{
MYSQL my_connection;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN];
int res;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"127.0.0.1"
,"用户","密码","数据名称",,NULL,))
{
perror("connect");
res=mysql_query(&my_connection,"select * from app");//查询
if(!res)
{
result=mysql_store_result(&my_connection);//保存查询到的数据到result
if(result)
{
int i,j;
printf("the result number is %lu\n ",(unsigned long)mysql_num_rows(result));
for(i=;fd=mysql_fetch_field(result);i++)//获取列名
{
bzero(column[i],sizeof(column[i]));
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=;i<j;i++)
{
printf("%s\t",sql_row[i]);
}
printf("\n");
} }
}
else
{
perror("select");
}
}
else
{
perror("connect:error");
}
mysql_free_result(MYSQL_RES *result);//释放结果资源
mysql_close(&my_connection);//断开连接 }
时间:
root@corleone:/opt/code/testC++/test/test5/test# ./mysql firstTime is:
共1485729个记录,每个记录23字段
secodnTime is:
remainTime is:
linux c调用 mysql代码的更多相关文章
- Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据
Title:Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据 --2013-10-11 11:57 #include <stdio.h> ...
- linux下python3调用c代码或者python3调用c++代码
前几篇的blog都是为了这个实验做基础,先说 原因是python调用数据库150w条数据22s,然后处理数据,其实就2个简单的for循环,65s 需求: 1. python调用c++函数 2. c++ ...
- PHP+MySQL代码部署在Linux(Ubuntu)上注意事项
最近帮同学做一个网站,同学买的是阿里云服务器,Linux发行版是Ubuntu12.04.我在本地把程序写好,都调试好了.然后他让我自己发布和部署.之前在大学里上操作系统课程时,也用过一段时间的Ubun ...
- PyQt5 中调用MySql接口失败 ( QSqlDatabase 组件) 在Linux环境下如何修改
最近在跑下面这么一个代码,怎么跑都无法连通服务器,如下: # -*- coding: utf-8 -*- ''' [简介] PyQt5中 处理database 例子 ''' import sys fr ...
- 【原】Linux环境下Shell调用MySQL并实现定时任务
对于一些周期性事务,我们可以在Linux下,使用shell脚本调用mysql数据库存储过程,并设置定时任务. 本来是要mysql数据库中创建事件任务来,定时执行存储过程,做数据传输的...使用cron ...
- python3.4学习笔记(二十五) Python 调用mysql redis实例代码
python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...
- Linux下C/C++代码调用PHP代码(转)
Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char * ...
- windows和linux环境下java调用C++代码-JNI技术
最近部门做安卓移动开发的需要调C++的代码,困难重重,最后任务交给了我,查找相关资料,没有一个教程能把不同环境(windows,linux)下怎么调用说明白的,自己在实现的过程中踩了几个坑,在这里总结 ...
- Ubuntu常用命令大全 以及 PHP+MySQL代码部署在Linux(Ubuntu)上注意事项
PHP+MySQL代码部署在Linux(Ubuntu)上注意事项 https://cloud.tencent.com/developer/article/1024187 Ubuntu常用命令大全 ht ...
随机推荐
- MySql中drop、truncate、delete的区别
1.drop:能对table和view 用法: drop table [is exists] 表1,表2,表3....: ①drop是DDL中删除表的操作,会删除表结构和所有数据,并释放空间. ②并 ...
- java加载类的顺序
一.什么时候会加载类?使用到类中的内容时加载:有三种情况1.创建对象:new StaticCode();2.使用类中的静态成员:StaticCode.num=9; StaticCode.show() ...
- BZOJ4218 : 不知道高到哪里去了
设$degi[x]$和$dego[x]$分别表示每个点的入度和出度,将线性规划的限制写出来: 目标函数: $\max.\ \sum_{x=1}^n(dego[x]P[x]-degi[x]Q[x])$ ...
- BZOJ3273 : liars
枚举每个人,计算他必定是诚实者的情况下至少有几个人说谎,若超过$t$则他肯定是说谎者. 对于至少有几个人说谎,区间信息可以合并: 每个区间维护最左最右两个人$l,r$以及$f[i][j]$表示$l$和 ...
- 通过 curl 传递数据
方法一(若为post方式,只适用于一维数组) /** * curl发送htpp请求 * 可以发送https,http,get方式,post方式,post数据发送 */ public function ...
- 在C#中,Json的序列化和反序列化的几种方式总结 转载
转载自 https://www.cnblogs.com/caofangsheng/p/5687994.html 谢谢 在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据 ...
- Linux之安装python
# yum install zlib-devel -y # tar -xf Python-3.6.0 cd Python-3.6.0 # ./configure --prefix=/usr/local ...
- Java知识回顾 (9) 同步、异步IO
一.基本概念 同步和异步: 同步和异步是针对应用程序和内核的交互而言的. 同步指的是用户进程触发IO 操作并等待或者轮询的去查看IO 操作是否就绪: 而异步是指用户进程触发IO 操作以后便开始做自己的 ...
- 【转】Wireshark和Fiddler分析Android中的TLS协议包数据(附带案例样本)
本文转自:http://www.wjdiankong.cn/wireshark%E5%92%8Cfiddler%E5%88%86%E6%9E%90android%E4%B8%AD%E7%9A%84tl ...
- Exchange Online Mailbox Restoration
User Account is already deleted in AD.User Mailbox is already deleted in Exchange. 1. Connect to Exc ...