代码:

#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代码的更多相关文章

  1. Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据

    Title:Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据 --2013-10-11 11:57 #include <stdio.h> ...

  2. linux下python3调用c代码或者python3调用c++代码

    前几篇的blog都是为了这个实验做基础,先说 原因是python调用数据库150w条数据22s,然后处理数据,其实就2个简单的for循环,65s 需求: 1. python调用c++函数 2. c++ ...

  3. PHP+MySQL代码部署在Linux(Ubuntu)上注意事项

    最近帮同学做一个网站,同学买的是阿里云服务器,Linux发行版是Ubuntu12.04.我在本地把程序写好,都调试好了.然后他让我自己发布和部署.之前在大学里上操作系统课程时,也用过一段时间的Ubun ...

  4. PyQt5 中调用MySql接口失败 ( QSqlDatabase 组件) 在Linux环境下如何修改

    最近在跑下面这么一个代码,怎么跑都无法连通服务器,如下: # -*- coding: utf-8 -*- ''' [简介] PyQt5中 处理database 例子 ''' import sys fr ...

  5. 【原】Linux环境下Shell调用MySQL并实现定时任务

    对于一些周期性事务,我们可以在Linux下,使用shell脚本调用mysql数据库存储过程,并设置定时任务. 本来是要mysql数据库中创建事件任务来,定时执行存储过程,做数据传输的...使用cron ...

  6. python3.4学习笔记(二十五) Python 调用mysql redis实例代码

    python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...

  7. Linux下C/C++代码调用PHP代码(转)

    Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char *  ...

  8. windows和linux环境下java调用C++代码-JNI技术

    最近部门做安卓移动开发的需要调C++的代码,困难重重,最后任务交给了我,查找相关资料,没有一个教程能把不同环境(windows,linux)下怎么调用说明白的,自己在实现的过程中踩了几个坑,在这里总结 ...

  9. Ubuntu常用命令大全 以及 PHP+MySQL代码部署在Linux(Ubuntu)上注意事项

    PHP+MySQL代码部署在Linux(Ubuntu)上注意事项 https://cloud.tencent.com/developer/article/1024187 Ubuntu常用命令大全 ht ...

随机推荐

  1. [JOI2017/2018]美術展

    [JOI2017/2018]美術展 题目大意: 有\(n(n\le5\times10^5)\)个物品,每个物品有两个属性:尺寸\(A_i\)和收益\(B_i\).从中选取一个子集,总收益为\(\sum ...

  2. React性能优化记录(不定期更新)

    React性能优化记录(不定期更新) 1. 使用PureComponent代替Component 在新建组件的时候需要继承Component会用到以下代码 import React,{Componen ...

  3. python正则表达式(一)

    ---恢复内容开始--- 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计 ...

  4. bzoj1531: [POI2005]Bank notes(多重背包)

    1531: [POI2005]Bank notes Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 521  Solved: 285[Submit][Sta ...

  5. Java 动态代理 Demo

    相比于静态代理,动态代理避免了开发者编写各个繁锁的静态代理类,只需指定一组接口及目标类对象就能动态地获取代理对象. 使用动态代理的六大步骤: 1 通过实现InvocationHandler接口来自定义 ...

  6. SqlHelper分享

    using Model; using System; using System.Collections.Generic; using System.Configuration; using Syste ...

  7. openstack 之~openstack各组件关系

    认识openstack 第一:openstack是什么? OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项 ...

  8. fzu1050 Number lengths(对数公式)

    http://acm.fzu.edu.cn/problem.php?pid=1050 cmath头文件里有两种对数log()和log10(),一个是自然对数,一个是以10为底, 求n!的位数,根据对数 ...

  9. jeffy-vim-v3.1.tar.gz

    下载链接: https://files.cnblogs.com/files/pengdonglin137/jeffy-vim-v3.1.tar.gz 1. 使用sublimemonokai配色 2. ...

  10. PPTP服务端与客户端 修改默认PPTP默认端口1723

    linux pptp服务端:我们在Linux下建立的pptpd端口号默认是1723,有时候这个端口并不是那么的好用,不是麽?所以服务端修改端口号比较简单 修改 /etc/services 文件查找 1 ...