MySql Connector/C++8结果集处理Demo

#include <iostream>
#include <exception>

#include <mysqlx/xdevapi.h>


using std::cout;
using std::endl;


int main(void)
try 
{
    //uri: mysqlx://user:password@host:port/db_name
    const char *from_uri = "mysqlx://root:mysql@localhost:33060/D_COMPANY?ssl-mode=disabled";
    mysqlx::Session sess(from_uri);

    mysqlx::SqlResult rset = sess.sql("SELECT * FROM T_DEPT").execute();

    mysqlx::Row row;
    while (rset.hasData()) { //判断结果集中是否有数据
        row = rset.fetchOne(); //从结果集中提取一行数据,同时此函数会自动指向下一行数据
        if (row.isNull()) { //判断此行是否为空
            cout << "row is null." << endl;
            break;
        }

        //提取行中每一列的数据,注意下标从 0 开始
        //使用C++中的类的强制转换
        cout << "  deptno: " << int(row.get(0)) << endl;
        cout << "deptname: " << std::string(row.get(1)) << endl;
        cout << " deptloc: " << std::string(row.get(2)) << endl;
    }

    sess.close();
    cout << "Done!" << endl;
}
catch (mysqlx::Error &err) 
{
    cout << "ERROR : " << err << endl;
    return -1;
}

猜你喜欢

转载自www.cnblogs.com/Focus-Flying/p/9316815.html