MySQL Connector/C++ 例子(二)
来源:互联网 发布:行程助手软件 编辑:程序博客网 时间:2024/06/05 04:37
/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; version 2 of the License.There are special exceptions to the terms and conditions of the GPLas it is applied to this software. View the full text of theexception in file EXCEPTIONS-CONNECTOR-C++ in the directory of thissoftware distribution.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA*//* Standard C++ includes */#include <stdlib.h>#include <iostream>/* 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 <cppconn/driver.h>#include <cppconn/exception.h>#include <cppconn/resultset.h>#include <cppconn/statement.h>#include <cppconn/prepared_statement.h>using namespace std;int main(void){cout << endl;cout << "Let's have MySQL count from 10 to 1..." << endl;try { sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::ResultSet *res; sql::PreparedStatement *pstmt; /* 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(); stmt->execute("DROP TABLE IF EXISTS test"); stmt->execute("CREATE TABLE test(id INT)"); delete stmt; /* '?' is the supported placeholder syntax */ pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)"); for (int i = 1; i <= 10; i++) { pstmt->setInt(1, i); pstmt->executeUpdate(); } delete pstmt; /* Select in ascending order */ pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC"); res = pstmt->executeQuery(); /* Fetch in reverse = descending order! */ res->afterLast(); while (res->previous()) cout << "\t... MySQL counts: " << res->getInt("id") << endl; delete res; delete pstmt; 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;
0 0
- MySQL Connector/C++ 例子(二)
- MySQL Connector/C++(一)
- MySQL Connector/C++ 例子(一)
- MySQL Connector/C++ 简单例子
- MySQL Connector C
- 安装部署(二) Ubuntu 安装配置 MySQL Connector Java
- Linux下安装使用mysql connector(C++)
- Linux下安装使用mysql connector(C++)
- HTTP Request Connector(二)
- 如何使用mysql connector c in window
- mysql Connector C/C++ 多线程封装
- mysql Connector C/C++ 多线程封装
- mysql Connector C/C++ 多线程封装
- VS2005 编译mysql-connector-c++-1.1.3
- mysql Connector C/C++ 多线程封装
- linux mysql-connector-c++-1.1.5 编译
- MySql C Connector 文档翻译 Overview
- c++连接mysql编程(mysq-connector/c++)
- 算法训练 明明的随机数
- javascript单例模式
- jQuery入门视频教程
- LeetCode Week 6: IPO
- [网易云课堂]Linux内核分析(六)—— 分析Linux内核创建一个新进程的过程
- MySQL Connector/C++ 例子(二)
- 2600 13号星期几?
- where,having与 group by连用的区别
- 整理:卷积的直观理解、物理意义与本质(四)
- 常见面试算法题:N个数中寻找仅出现过1次的数
- 算法: 快速求中位数(第k大数)
- openVR驱动接口之IServerDriverHost简介
- docker 启动安装等命令
- 关于get和post网络请求的方式和formid的应用。