c++读取mysql数据库结果保存
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <mysql/mysql.h>
#include <stdlib.h> using namespace std; //数据库地址密码
string g_Server = "localhost";
string g_User = "root";
string g_Password = "";
string g_Database = "test";
//vector 输出
void printVevtorVal(vector <string> strVal)
{
vector<string>::iterator itr = strVal.begin();
for(itr;itr!=strVal.end();itr++)
{
cout << (*itr) << " ";
}
cout << endl;
} //vector二维表 输出
void printTableVal(vector <vector <string> > strVal)
{
vector <vector <string> >::iterator itr = strVal.begin();
for(itr;itr!=strVal.end();itr++)
{
vector<string>::iterator tmp = (*itr).begin();
for(tmp;tmp!=(*itr).end();tmp++)
{
cout << setw() << left << (*tmp);
}
cout << endl;
} } //查询数据库结果只有一个值
string getStrValSql(string strSql)
{
string strvals = "";
MYSQL *conn;
MYSQL_RES *res;
conn = mysql_init(NULL); if (!mysql_real_connect(conn, g_Server.c_str(),g_User.c_str(), g_Password.c_str(), g_Database.c_str(),, NULL, ))
{
return "";
}
mysql_query(conn, strSql.c_str()); //执行sql语句
res = mysql_store_result(conn); //将查询结果装进MYSQL_RES
if(!res) //sql执行结果判断
{
return "";
}
int rows = mysql_num_rows(res); //获取结果行数
while(rows--)
{
MYSQL_ROW row = mysql_fetch_row(res); //从结果集中获取一行
strvals = (row[] == NULL ? "":row[]);
}
mysql_free_result(res); //查询完后记得要释放
mysql_close(conn); return strvals;
}
//数据库查询只有一行或者一列,返回vector
void getVecValSql(string strSql, vector<string> &vecVals)
{
MYSQL *conn;
MYSQL_RES *res;
conn = mysql_init(NULL); if (!mysql_real_connect(conn, g_Server.c_str(),g_User.c_str(), g_Password.c_str(), g_Database.c_str(),, NULL, ))
{
return ;
}
mysql_query(conn, strSql.c_str());
res = mysql_store_result(conn);
if(!res)
{
return ;
}
int rows = mysql_num_rows(res); if( rows == )
{
int cols = mysql_num_fields(res);
int intTmp = ;
MYSQL_ROW row = mysql_fetch_row(res);
while(intTmp < cols)
{
vecVals.push_back(row[intTmp] == NULL ? "":row[intTmp]);
intTmp++;
}
}
else
{
while(rows--)
{
MYSQL_ROW row = mysql_fetch_row(res);
vecVals.push_back(row[] == NULL ? "":row[]);
}
}
mysql_free_result(res);
mysql_close(conn);
} //数据库查询返回二维表
void getTableValSql(string strSql,vector <vector <string> > &vecVals)
{
MYSQL *conn;
MYSQL_RES *res;
conn = mysql_init(NULL); if (!mysql_real_connect(conn, g_Server.c_str(),g_User.c_str(), g_Password.c_str(), g_Database.c_str(),, NULL, ))
{
return ;
}
mysql_query(conn, strSql.c_str());
res = mysql_store_result(conn);
if(!res)
{
return ;
}
int rows = mysql_num_rows(res);
int cols = mysql_num_fields(res);
while(rows--)
{
int intTmp = ;
vector <string> tmp; MYSQL_ROW row = mysql_fetch_row(res);
while(intTmp < cols)
{
tmp.push_back(row[intTmp] == NULL ? "":row[intTmp]);
intTmp++;
}
vecVals.push_back(tmp);
} mysql_free_result(res);
mysql_close(conn);
}

linux C++ 读取mysql结果保存的更多相关文章

  1. Spark使用Java读取mysql数据和保存数据到mysql

    原文引自:http://blog.csdn.net/fengzhimohan/article/details/78471952 项目应用需要利用Spark读取mysql数据进行数据分析,然后将分析结果 ...

  2. Linux下PHP+MySQL+CoreSeek中文检索引擎配置

    说明: 操作系统:CentOS 5.X 服务器IP地址:192.168.21.127 Web环境:Nginx+PHP+MySQL 站点根目录:/usr/local/nginx/html 目的:安装co ...

  3. Linux下的Mysql的主从备份

    MySQL复制概述 MySQL数据库支持同步复制.单向.异步复制,在复制的过程中一个服务器充当主服务,而一个或多个服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循 ...

  4. Linux下查看mysql、apache是否安装,安装,卸载等操作

    Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root               ?        :: /bin/sh /usr/ ...

  5. 【linux操作命令】mysql

    1.linux下启动mysql的命令: mysqladmin start /ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: mys ...

  6. 基于linux c的mysql操作——幼儿园数据管理系统

    上周对于mysql进行了简单的学习,利用c对mysql进行操作,主要用到了以下几个函数: mysql_init(); mysql_real_connect(数据库变量指针,网络地址,用户名,登录密码, ...

  7. Linux下查看MySQL的安装路径

    Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root               ?        :: /bin/sh /usr/ ...

  8. Linux JDK+TOMCAT+MYSQL+redis 安装日志

    检查是否安装iptables #先检查是否安装了iptablesservice iptables status#安装iptablesyum install -y iptables#升级iptables ...

  9. windows下数据库文件使用脚本同步到linux下的mysql数据库中

    1.背景 windows server 2008 下 每天会有 *.sql数据文件 需要上传到linux 中的mysql数据库中 而运维人员是在 windows server 下使用 xshell 连 ...

随机推荐

  1. pytorch之 classification

    import torch import torch.nn.functional as F import matplotlib.pyplot as plt # torch.manual_seed(1) ...

  2. 使用docker搭建FastDFS

    拉取镜像(使用docker-componse可以忽略) [root@localhost ~]# docker pull phinexdaz/fdfs_tracker [root@localhost ~ ...

  3. a标签没有闭合引起自动插入很多a标签的问题

    a标签中间没有内容的情况下,很容易忽略闭合 a标签一定要闭合,否则会在后面每个div后面插入同一个a标签 要以如下形式闭合: <div class="v5-index-containe ...

  4. IntelliJ 更改项目使用的 JDK 版本

    在当前使用的 IntelliJ 中的 JDK 版本为 1.8,如何修改 IntelliJ 使用的 JDK 版本为 1.11 呢? 你可以在 IntelliJ 中进行修改. 选择 File 后,然后选择 ...

  5. 手动使用I2C协议写入24C02C

    刚尝试用AT89C52单片机使用IIC总线协议读写AT24C02C,我忽然想能否用手动调整开关的方式写入AT24C02C?于是,便尝试了一下,结果果然成功了. 关于IIC总线,这篇文章写的很详细:ht ...

  6. Python3标准库:functools管理函数的工具

    1. functools管理函数的工具 functools模块提供了一些工具来调整或扩展函数和其他callable对象,从而不必完全重写. 1.1 修饰符 functools模块提供的主要工具就是pa ...

  7. 下拉菜单的jquery组件封装

    首先晒出封装好的dropdown.js (function($){ 'use strict';//使用严格模式 //构造函数形式 function Dropdown(elem,options){ // ...

  8. SQL Server等待事件—PAGEIOLATCH_EX

    什么是PAGEIOLATCH_EX等待事件? 下面我们将对PAGEIOLATCH_EX等待事件的相关资料做一个简单的归纳.整理.关于PAGEIOLATCH_EX,官方文档的简单介绍如下: PAGEIO ...

  9. 转载:字符编码简介 ASCII UTF-8 ISO8859-1

    字符编码简介 ASCII UTF-8 ISO8859-1 博客分类: 电脑综合知识 XP数据结构Windows  计算机中的一切都是以数字来表示的,字符同样如此.字符编码就是将字符集编码成为数字序列, ...

  10. Codeforces Round 450 D 隔板法+容斥

    题意: Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers ...