linux C++ 读取mysql结果保存

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 = "467"; 
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(10) << 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(),0, NULL, 0))
    {
      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[0] == NULL ? "":row[0]);        
    }
    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(),0, NULL, 0))
    {
      return ;
    }     
    mysql_query(conn, strSql.c_str());       
    res = mysql_store_result(conn);      
    if(!res)                                
    {
        return ;
    }
    int rows = mysql_num_rows(res);               
    
    if( rows == 1)
    {    
        int cols = mysql_num_fields(res);
        int intTmp = 0;
        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[0] == NULL ? "":row[0]);        
        }                
    }
    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(),0, NULL, 0))
    {
      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 = 0;
        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);  
}

 

posted @ 2019-04-10 22:10  茫茫明明  阅读(686)  评论(0编辑  收藏  举报