reference_counting.cc

来自「有关MYSQL的开源码」· CC 代码 · 共 57 行

CC
57
字号
#include <mysqlcppapi/mysqlcppapi.h>#include <iostream>#include <iomanip>//This tests the reference-counting of shared connections, queries, and results.//The sharing happens when copy constructors are used.//The actual underlying resources should not be released until the last client C++ instance has died.int main(){   try  {    mysqlcppapi::Connection connection;    connection.set_Host("localhost");    connection.set_User("testuser");    connection.set_Password("testpassword");    for(int i = 0; i < 3; ++i)    {      std::cout << "iteration: " << i << std::endl;            connection.connect();            mysqlcppapi::Connection connection_copy = connection; //Test sharing of underlying connection;            connection.select_database("test");      {        mysqlcppapi::Query query = connection.create_Query();        query << "select * from staff";        mysqlcppapi::Result_Store res = query.store();      }            {        //We actually use connection_copy to make sure that the compiler doesn't optimise it away.        mysqlcppapi::Query query = connection_copy.create_Query();        mysqlcppapi::Query query2 = query; //Test sharing of underlying query;        query << "select * from departments";        mysqlcppapi::Result_Store res = query.store();        mysqlcppapi::Result_Store res2 = res; //Test sharing of underlying result;      }      connection.close();    }  }  catch(mysqlcppapi::ex_base& er)  {    // handle any connection or query errors that may come up    std::cerr << "Exception: " << er.what() <<  std::endl;    return -1;  }    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?