#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;
}