#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;
#ifdef OTHER
cout << " deptno: " << row.get(0).get<int>() << endl;
cout << "deptname: " << row.get(1).get<std::string>() << endl;
cout << " deptloc: " << row.get(2).get<std::string>() << endl;
#endif
}
sess.close();
cout << "Done!" << endl;
}
catch (mysqlx::Error &err)
{
cout << "ERROR : " << err << endl;
return -1;
}