20vs连接mysql需要驱动吗_使用Connector/C++(VS2015)连接MySQL的完整例子

/*Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; version 2 of the License.

There are special exceptions to the terms and conditions of the GPL

as it is applied to this software. View the full text of the

exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this

software distribution.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License

along with this program; 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#include

using namespacestd;int main(void)

{

cout<

cout<< "Let's have MySQL count from 10 to 1..." <

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)");deletestmt;/*'?' 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();

}deletepstmt;/*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") <

}catch (sql::SQLException &e) {

cout<< "# ERR: SQLException in" <<__file__>

cout<< "(" << __FUNCTION__ << ") on line" << __LINE__ <

cout<< "# ERR:" <

cout<< "(MySQL error code:" <

cout<< ", SQLState:" << e.getSQLState() << ")" <

}

cout<

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值