C++ 获取每一行的数据 FetchRow() 用vector()实现

1、在LXData里面定义结构体

    struct LXAPI LXData
	{
		const char* data = 0;
		int size = 0;
	};

2、在LXMysql.h和LXMysql.cpp分别定义和编写代码

//获取一行数据
	std::vectorFetchRow();
std::vector LXMysql::FetchRow()
	{
		std::vectorre;
		if (!result)
		{
			return re;
		}
		MYSQL_ROW row = mysql_fetch_row(result);
		if (!row)
		{
			return re;
		}
		//列数,每一个数据的大小
		int fnum = mysql_num_fields(result);
		unsigned long* lens = mysql_fetch_lengths(result);
		for (int i = 0;i < fnum;i++)
		{
			LXData data;
			data.data = row[i];
			data.size = lens[i];
			re.push_back(data);
		}
		return re;
	}

3、测试,在test_LXMysql.cpp里面测试

    //4、获取结果集
    sql = "insert into t_vedio (name) values ('test0001')";
    //多插入几个数据
    cout << my.Query(sql.c_str()) << endl;
    cout << my.Query(sql.c_str()) << endl;
    cout << my.Query(sql.c_str()) << endl;
    cout << my.Query(sql.c_str()) << endl;
    sql = "select *from t_vedio";
    cout << my.Query(sql.c_str()) << endl;

    my.StoreResult();//结果集本地全部存储
    for (;;)
    {
        auto row = my.FetchRow();
        if (row.size() == 0)
        {
            /*cout << "cedw" << endl;*/
            break;
        }
        row[2].SaveFile(row[1].data);
        for (int i = 0;i < row.size();i++)
        {
            if (row[i].data)
            {
                cout << row[i].data << " ";
            }
        }
        cout << endl;
    }

    my.FreeResult();


    cout << my.Query(sql.c_str()) << endl;
    my.UseResult();//开始接收结果集
    my.FreeResult();

你可能感兴趣的:(c++,开发语言,mysql,动态链接库,数据库)