c++读写mysql

// mysql.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
using namespace std;

int main()
{
    MYSQL m_sql;
    mysql_init(&m_sql);
    if(!mysql_real_connect(&m_sql, "localhost", "root", "zxc000", "mdb", 3306, NULL, 0))
    {
        cout << "连接失败" << endl;
    }
    //插入数据
    /*
    char insert[200] = "insert into student values(2000,'张','男',22,'cs')";
    mysql_query(&m_sql,"set names 'gbk'");  //设置插入字符集
    mysql_query(&m_sql, insert);
    */
    //修改数据
    char update[200] = "update student set Sno=2001 where Sno=2000";
    mysql_query(&m_sql, "set names 'gbk'");  //设置字符集
    mysql_query(&m_sql, update);

    char select[200] = "select * from student";
    mysql_query(&m_sql, "set names 'gb2312'");   //设置客户端字符集
    mysql_query(&m_sql, select);
    MYSQL_RES *result;
    result = mysql_store_result(&m_sql);
    //打印列名
    MYSQL_FIELD *fields;
    fields = mysql_fetch_fields(result);  //返回所有列
    auto num = mysql_num_fields(result);
    for (auto j = 0; jcout << fields[j].name << "  ";
    }
    cout << endl;
    //打印表
    MYSQL_ROW row;
    unsigned int i, num_c;
    num_c = mysql_num_fields(result);  //列数
    while(row=mysql_fetch_row(result))
    {
       for(i=0;icout << row[i] <<"  ";
       }
       cout << endl;
    }
    mysql_free_result(result);
    return 0;
}

你可能感兴趣的:(数据库)