当前位置:   article > 正文

mysql connector c++ 使用_MySQL Connector/C++ 使用 | 学步园

mysql connector for c++ 32位

1. 到MySQL官网( http://dev.mysql.com/downloads/connector/cpp/ )

下载 mysql-connector-c++-noinstall-1.0.5-win32-vs2005

2. 安装MySQL, 注意安装前要勾上C/C++ Headers 这个选项

3. 建立工程,把 libmySQL.dll、mysqlcppconn.dll 放到工程根目录下

4. 代码如下:

/* Standard C++ headers */

#include

#include

/* MySQL Connector/C++ headers */

#include

#include

#include

#include

#include

#pragma comment(lib, "mysqlcppconn.lib")

/* Microsoft headers */

#include

#define __free__(p, b_close)/

if (b_close) { p->close(); } if (p != NULL) { delete p; p = NULL; }/

struct cStudent

{

int nID;

std::string s_name;

int nAge;

};

int _tmain(int argc, _TCHAR* argv[])

{

sql::Driver *driver = NULL;

sql::Connection *conn = NULL;

sql::Statement *stmt = NULL;

sql::ResultSet *rs = NULL;

sql::Savepoint *savept = NULL;

try

{

driver = get_driver_instance();

conn = driver->connect("tcp://127.0.0.1:3306", "root", "root");

// 设置为手动提交

conn->setAutoCommit(false);

conn->setSchema("test");

savept = conn->setSavepoint("SAVE_POINT");

stmt = conn->createStatement();

char buf[64];

for (int i = 0; i < 100; ++i)

{

sprintf(buf, "INSERT INTO student(name, age) VALUES(/"tom%d/", %d);", i + 1, i + 19);

stmt->execute(buf);

}

rs = stmt->executeQuery("SELECT * FROM student LIMIT 10");

std::vector students;

while (rs->next())

{

cStudent student;

student.nID = rs->getInt("id");

student.s_name = rs->getString("name");

student.nAge = rs->getInt("age");

students.push_back(student);

}

// 提交

conn->commit();

}

catch (sql::SQLException &e)

{

std::string s_error(e.what());

std::cout << e.what() << std::endl;

// 回滚操作

conn->rollback(savept);

conn->releaseSavepoint(savept);

}

// 释放资源

__free__(rs, false);

__free__(stmt, false);

__free__(conn, true);

system("PAUSE");

return 0;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/676539
推荐阅读
相关标签
  

闽ICP备14008679号