当前位置:   article > 正文

MySQL Connector/C++ 操作MySQL数据库(配置成功,本人在用)_mysqlcppconn-7-vs14.dll下载

mysqlcppconn-7-vs14.dll下载

一、官网下载https://dev.mysql.com/downloads/connector/cpp/源码生成需要cmake编译,本人直接下载了生成好的lib、dll、.h文件直接使用:

解压后:

二、配置(vs2017)

1、项目/属性/(C/C++)/常规/附加包含目录 中添加.h文件所在的include文件夹。

2、项目/属性/链接器/常规/附加库目录 加入.lib所在的文件夹。debug和release加入各自对应的文件夹。

3、项目/属性/链接器/输入/附加依赖项 加入.lib文件。

4、将mysqlcppconn-7-vs14.dll拷贝至程序根目录下,还需要将libeay32.dll和ssleay32.dll也拷贝至程序根目录下,否则会报错。

5、程序用到了boost库智能指针,下载boost库,解压后为boost_1_64_0,程序中附加包含目录引入即可,否则会报错。

6、程序引入头文件

  1. #include "jdbc/mysql_connection.h"
  2. #include "jdbc/mysql_driver.h"
  3. #include "jdbc/cppconn/statement.h"

7、连接数据库

  1. //初始化驱动
  2. sql::mysql::MySQL_Driver *driver = NULL;
  3. sql::Connection *con = NULL;
  4. driver = sql::mysql::get_mysql_driver_instance();
  5. if (driver == NULL)
  6. {
  7. cout << "driver is null" << endl;
  8. }
  9. con = driver->connect("tcp://localhost:3306", "root", "root");
  10. if (con == NULL)
  11. {
  12. cout << "conn is null" << endl;
  13. }
  14. cout << "connect suceess" << endl;

8、程序使用mysql版本为mysql-5.6.24-win32,完整的代码如下:

  1. #include "iostream"
  2. #include "jdbc/mysql_connection.h"
  3. #include "jdbc/mysql_driver.h"
  4. #include "jdbc/cppconn/statement.h"
  5. #include "jdbc/cppconn/prepared_statement.h"
  6. using namespace std;
  7. using namespace sql;
  8. int main()
  9. {
  10. //初始化驱动
  11. sql::mysql::MySQL_Driver *driver = NULL;
  12. sql::Connection *conn = NULL;
  13. driver = sql::mysql::get_mysql_driver_instance();
  14. if (driver == NULL)
  15. {
  16. cout << "driver is null" << endl;
  17. }
  18. //连接
  19. //con = driver->connect("tcp://localhost:3306", "root", "root");
  20. conn = driver->connect("tcp://localhost:3306/ourcms", "root", "root");
  21. if (conn == NULL)
  22. {
  23. cout << "conn is null" << endl;
  24. }
  25. cout << "connect suceess" << endl;
  26. //查询
  27. int flag = 0;
  28. sql::Statement *stmt = conn->createStatement();
  29. sql::ResultSet *res;
  30. res = stmt->executeQuery("SELECT * FROM cms_device");
  31. while (res->next())
  32. {
  33. cout << res->getInt("id") << endl;
  34. cout << res->getString("phone").c_str() << endl;
  35. cout << res->getString("imsi").c_str() << endl;
  36. }
  37. //插入
  38. conn->setAutoCommit(0);//关闭自动提交
  39. PreparedStatement *prep_stmt;
  40. int updatecount = 0;
  41. res->first();
  42. flag = 0;
  43. while (res->next())
  44. {
  45. if (strcmp(res->getString("imsi").c_str(), "460010010000100") == 0)
  46. {
  47. flag = 1;
  48. break;
  49. }
  50. }
  51. if (flag == 0) {
  52. prep_stmt = conn->prepareStatement("INSERT INTO cms_device (id,phone,imsi) VALUES (111,?,?)");
  53. prep_stmt->setString(1, "15043214321");
  54. prep_stmt->setString(2, "460010010000100");
  55. updatecount = prep_stmt->executeUpdate();
  56. }
  57. Savepoint *savept;
  58. savept = conn->setSavepoint("SAVEPT1");
  59. res->first();
  60. flag = 0;
  61. while (res->next())
  62. {
  63. if (strcmp(res->getString("imsi").c_str(), "460010010000101") == 0)
  64. {
  65. flag = 1;
  66. break;
  67. }
  68. }
  69. if (flag == 0) {
  70. prep_stmt = conn->prepareStatement("INSERT INTO cms_device (phone,imsi) VALUES (?,?)");
  71. prep_stmt->setString(1, "15043214321");
  72. prep_stmt->setString(2, "460010010000101");
  73. updatecount = prep_stmt->executeUpdate();
  74. }
  75. conn->rollback(savept);
  76. conn->releaseSavepoint(savept);
  77. conn->commit();
  78. //更新
  79. conn->setAutoCommit(1);//打开自动提交
  80. prep_stmt = conn->prepareStatement("update cms_device set phone=? where phone=?");
  81. prep_stmt->setString(1, "15011111111");
  82. prep_stmt->setString(2, "15043214321");
  83. updatecount = prep_stmt->executeUpdate();
  84. }

 

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

闽ICP备14008679号