包含头文件

#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/exception.h>#ifdef _DEBUG
#pragma comment(lib, "mysqlcppconn.lib")
#else
#pragma comment(lib, "mysqlcppconn-static.lib")
// 我的MySQL connector/C++是自己下源码编译的,需要引入这个,官方直接提供的二进制我不清楚需要不需要
#pragma comment(lib, "mysqlclient.lib")
#endif

代码

try{const char* user = "root";const char* passwd = "123456";const char* host = "tcp://192.168.1.8:3306";const char* database = "mysql";sql::mysql::MySQL_Driver* driver = sql::mysql::get_driver_instance();sql::Connection* conn = driver->connect(host, user, passwd);conn->setSchema(database);sql::Statement *stmt = conn->createStatement();sql::ResultSet *res = stmt->executeQuery("select * from user;");while (res->next()) {AfxMessageBox((res->getString(2) + " | " + res->getString(3)).c_str());}delete res;delete stmt;delete conn;}catch (sql::SQLException e) {CString strErrorMsg;strErrorMsg.Format("MySQL error code %d: %s, %s", e.getErrorCode(), e.what(), e.getSQLState().c_str());AfxMessageBox(strErrorMsg);if (e.getErrorCode() == 1047) {AfxMessageBox("1047");}}catch (std::runtime_error e) {AfxMessageBox(e.what());}

静态链接

1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) class sql::mysql::MySQL_Driver * __cdecl sql::mysql::get_driver_instance(void)" (__imp_?get_driver_instance@mysql@sql@@YAPAVMySQL_Driver@12@XZ)
1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: virtual __thiscall sql::SQLException::~SQLException(void)" (__imp_??1SQLException@sql@@UAE@XZ)
1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@QBEHXZ)
1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall sql::SQLException::getSQLState(void)const " (__imp_?getSQLState@SQLException@sql@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: char const * __thiscall sql::SQLString::c_str(void)const " (__imp_?c_str@SQLString@sql@@QBEPBDXZ)
1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??0SQLString@sql@@QAE@QBD@Z)
1>TestDlg.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QAE@XZ)

  

通过观察cppconn/build_config.h得知若要得到非__declspec(dllimport)方式的引入需要定义宏CPPCONN_LIB_BUILD

解决方案

在包含头文件前定义宏CPPCONN_LIB_BUILD,告诉链接器MySQL connector是静态库方式编译的即可

#ifndef _DEBUG
#define CPPCONN_LIB_BUILD
#endif#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/exception.h>
#ifdef _DEBUG
#pragma comment(lib, "mysqlcppconn.lib")
#else
#pragma comment(lib, "mysqlcppconn-static.lib")
// 我的MySQL connector/C++是自己下源码编译的,需要引入这个,官方直接提供的二进制我不清楚需要不需要
#pragma comment(lib, "mysqlclient.lib")
#endif

转载于:https://www.cnblogs.com/sankeyou/p/4054472.html

vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错相关推荐

  1. Windows环境下Code::Blocks中成功配置MySQL Connector/C连接MySQL数据库

    下面我将介绍当需要用C语言开发能访问MySQL数据库的程序时成功配置的关键事项. 操作系统:Windows 7 x64 编程环境:Code::Blocks 10.05(32bit) 配置方式说明:用C ...

  2. django mysql connector,MySQL Connector / Python作为Django引擎?

    即使经过数小时和数小时的谷歌搜索,也无法找到答案.搜索堆栈溢出.我向你们保证,我已经看到了所有可能被视为相关的答案,但这些答案都没有解决我所面临的问题.无需再费周折 – 目前在shell中我可以这样做 ...

  3. linux mysql connector_fd_在CentOS里使用MySQL Connector/C++

    操作系统版本:CentOS6 64位 1,安装boost库.因为MySQL Connector/C++使用了boost库,所以必须先安装boost库,我们才能使用MySQL Connector/C++ ...

  4. Developing DataBase Applications Using MySQL Connector/C++ 中文文本

    日期 备注 2020年6月3日 对排版进行了调整.   翻译自mysql Connector C++帮助文档[http://download.csdn.net/detail/midle110/4931 ...

  5. django mysql connector,MySQL Connector / python在Django中不起作用

    我正在学习以MySQL为后端的Django. 我安装了Oracle的mysql连接器以与mysql连接. 但是,当我运行python manage.py时,出现此错误 Traceback (most ...

  6. MySQL Connector/Net 5.20安装后无法在VS2008中正常使用的问题

    安装了MySQL Connector/Net 5.20之后在VS2008中新建连接,居然直接报告错误 Package Load Failure Package 'MySQL Connector Net ...

  7. 解决金仓数据库安装时安装VC++2013报错问题:不受信任提供程序信任的根证书中终止

    解决金仓数据库安装时安装VC++2013报错问题:不受信任提供程序信任的根证书中终止 安装微软的信任证书: 1.点击链接下载微软证书:http://download.microsoft.com/dow ...

  8. mysql中为表增加外键_如何在Excel 2013中为符号分配键盘快捷键

    mysql中为表增加外键 We've previously shown you how to add keyboard shortcuts to symbols in Word 2013 to mak ...

  9. VS2019中接连MySQL全部过程

    VS2019中接连MySQL 连接MySQL的教程 添加MySQL的引用 环境配置 测试代码 在vs里面运行sql语句 连接MySQL的教程 如果要在 Visual Studio 2019中使用MyS ...

最新文章

  1. 经典网络LeNet-5介绍及代码测试(Caffe, MNIST, C++)
  2. 【图文讲解】TCP为啥要3次握手和4次挥手?握两次手不行吗?
  3. php extension 安装,php + clucene extension的安装
  4. 如何正确地修改.data和.item文件的‘utf-8’格式
  5. javascript中函数和方法的区别
  6. linux运行星际争霸1
  7. php cdi_教程:编写自己的CDI扩展
  8. Java 8快多少?
  9. WORD常用版式保存为模板?
  10. java 记录用户_JavaWeb学习记录(六)——用户登录功能
  11. IE下 jqModal的问题
  12. 软件工程基础作业 可行性与需求分析
  13. win7 便签快捷键
  14. 约翰·冯·诺依曼的开挂人生
  15. 【高速PCB电路设计】2.高速电路DDR原理图概述
  16. 1.分布式服务架构:原理、设计与实战 --- 分布式微服务架构设计原理
  17. 幸福工厂(Satisfactory)中文破解版
  18. 2019年8月8日 星期四 今日计划
  19. 索隆:九山八海,无我不断者。
  20. USB的红绿黑白线详解

热门文章

  1. C语言中int类型及位、字节和字的介绍
  2. 小米物联网世界第一_世界最大物联网?小米牵手宜家布局AloT,却因它栽了大跟头?...
  3. Python程序设计 第4章:复合数据类型
  4. js 控制台调试——console 对象【详解】
  5. JAVA+SQL办公自动化系统(源代码+论文+外文翻译)
  6. 数据结构——带头结点双向循环链表
  7. 尝试写个UC浏览器(主页交互篇)
  8. System x3650 M5 U盘安装系统
  9. 企立方:拼多多如果退店要怎么做
  10. 什么是范数(Norm),其具有哪些性质