相关的文档10bet官方网站 本手册下载
PDF (Ltr)- 285.1 kb
PDF (A4)- 286.0 kb


6.5完整的示例1

下面的代码显示了一个完整的例子如何使用连接器/ c++。

/ *版权2008、2010、Oracle和/或其附属公司。这个程序是自由软件;你可以重新分配和/或修改它根据GNU通用公共许可证是由自由软件基金会发布;版本2的许可证。有特殊的例外条款和条件的GPL是应用于该软件。查看全文的异常文件EXCEPTIONS-CONNECTOR-C + +目录的软件分发。这个程序是分布式的,希望会有用,但没有任何担保;甚至没有隐含保证适销性或健身为特定目的。详细信息,请参阅GNU通用公共许可证。你应该已经收到了GNU通用公共许可证的副本连同这个项目; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /* Standard C++ includes */ #include  #include  /* Include directly the different headers from cppconn/ and mysql_driver.h + mysql_util.h (and mysql_connection.h). This will reduce your build time! */ #include "mysql_connection.h" #include  #include  #include  #include  using namespace std; int main(void) { cout << endl; cout << "Running 'SELECT 'Hello World!' » AS _message'..." << endl; try { sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::ResultSet *res; /* Create a connection */ driver = get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); /* Connect to the MySQL test database */ con->setSchema("test"); stmt = con->createStatement(); res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); while (res->next()) { cout << "\t... MySQL replies: "; /* Access column data by alias or column name */ cout << res->getString("_message") << endl; cout << "\t... MySQL says it again: "; /* Access column data by numeric offset, 1 is the first column */ cout << res->getString(1) << endl; } delete res; delete stmt; delete con; } catch (sql::SQLException &e) { cout << "# ERR: SQLException in " << __FILE__; cout << "(" << __FUNCTION__ << ") on line " » << __LINE__ << endl; cout << "# ERR: " << e.what(); cout << " (MySQL error code: " << e.getErrorCode(); cout << ", SQLState: " << e.getSQLState() << " )" << endl; } cout << endl; return EXIT_SUCCESS; }