您好,欢迎访问一九零五行业门户网

C++中使用MySQL_MySQL

我的电脑上的软件如下:
vs2012
mysql 5.6
操作系统是64位win8.1
1.前提
下载并安装mysql
2.配置
(1)在项目中 属性》c++》常规》添加附加包含目录:my_sql的目录/include
(2)在项目中 属性》连接器》常规》添加附加库目录:my_sql的目录/lib
(3)在项目中 属性》连接器》输入》添加附加依赖项:libmysql.lib
3.注意事项
如果已经按上述配置好但是还是编译不通过,提示:
1> 正在生成代码...
1>basedb.obj : error lnk2019: 无法解析的外部符号 _mysql_init@4,该符号在函数 public: bool __thiscall basedb::openconnect(void) (?openconnect@basedb@@qae_nxz) 中被引用1>basedb.obj : error lnk2019: 无法解析的外部符号 _mysql_real_connect@32,该符号在函数 public: bool __thiscall basedb::openconnect(void) (?openconnect@basedb@@qae_nxz) 中被引用1>basedb.obj : error lnk2019: 无法解析的外部符号 _mysql_query@8,该符号在函数 public: struct st_mysql_res * __thiscall basedb::doquery(class std::basic_string,class std::allocator > const &) (?doquery@basedb@@qaepaust_mysql_res@@abv?$basic_string@du?$char_traits@d@v?$allocator@d@@z) 中被引用1>basedb.obj : error lnk2019: 无法解析的外部符号 _mysql_store_result@4,该符号在函数 public: struct st_mysql_res * __thiscall basedb::doquery(class std::basic_string,class std::allocator > const &) (?doquery@basedb@@qaepaust_mysql_res@@abv?$basic_string@du?$char_traits@d@v?$allocator@d@@z) 中被引用1>basedb.obj : error lnk2019: 无法解析的外部符号 _mysql_close@4,该符号在函数 public: void __thiscall basedb::closeconnect(void) (?closeconnect@basedb@@qaexxz) 中被引用1>dbutil.obj : error lnk2019: 无法解析的外部符号 _mysql_free_result@4,该符号在函数 public: static bool __cdecl dbutil::checkuserlogin(class std::basic_string,class std::allocator > const &,class std::basic_string,class std::allocator > const &) (?checkuserlogin@dbutil@@sa_nabv?$basic_string@du?$char_traits@d@v?$allocator@dz) 中被引用1>e:/flighting/mysqltest/consoleapplication1/debug/consoleapplication1.exe : fatal error lnk1120: 6 个无法解析的外部命令1>1>生成失败。
这个问题的产生原因是:你装的mysql64位的,而vs2012默认的运行平台是32位的所以不支持
解决办法有两个:(1)项目属性》右上角有一个配置管理器》把你的项目的运行平台改为x64(如果没有就新建一个)
(2)如果项目中已经引入了其他32位的附加库,贸然修改成64位会导致原来的库编译不通过,这样没办法只好上网搜一个mysql32为的lib和dll
最后,如果运行时提示找不到libmysql.dll 就把libmysql,dll复制到system32/syswow64 吧
代码实例:
#include #include #include #include #include #include using namespace std;int main(){ const char user[] = root; //username const char pswd[] = root; //password const char host[] = localhost; //or127.0.0.1 const char table[] = test; //database unsigned int port = 3306; //server port mysql mycont; mysql_res *result; mysql_row sql_row; mysql_field *fd; char column[32][32]; int res; mysql_init(&mycont); if(mysql_real_connect(&mycont,host,user,pswd,table,port,null,0)) { cout<
其它类似信息

推荐信息