Mysql For Windows c++

来源:互联网 发布:淘宝属于什么行业分类 编辑:程序博客网 时间:2024/05/18 15:29

网上多是关于的开发

 

开发环境 xpsp3 boost_1_48_0 STLport-5.2.1 mysql 5.5.20 server

 

windows 下装好 mysql server

 MySQL 的安装目录下 会有几个文件夹,其中

Connector C++ 1.1.0 //这个c++

Connector C 6.0.2 //c

还有java  .net  ... 当然也有python 之类的...

 

如果没有可以下载于:

http://www.mysql.com/downloads/connector/cpp/

 

到此,还少一个东西 sqlstring.h(我不知道为什么它不提供)

在这里下载

http://download.csdn.net/detail/wangxvfeng101/4056020

 

请先参考一下网上的mysql 教程(这不在本文之内介绍)

 

测试条件:

数据库 wxf 中的表 mytable 内容如下:

+-------+------+------------+-----------+

| name  | sex  | birth      | birthaddr |

+-------+------+------------+-----------+

| abccs | f    | 1977-07-07 | china     |

| aaa   | f    | 1987-08-27 | beijin    |

+-------+------+------------+-----------+

 

//测试工程:

#include <iostream>#include <stdio.h>#include <mysql_connection.h>#include <mysql_driver.h>#include <cppconn/driver.h>#include <cppconn/exception.h>#include <cppconn/resultset.h>#include <cppconn/statement.h>#include <cppconn/prepared_statement.h>using namespace std;using namespace sql; #pragma comment(lib,"mysqlcppconn.lib") void RunConnectMySQL(){sql::mysql::MySQL_Driver *driver = NULL;sql::Connection *con = NULL;Statement *state = NULL;ResultSet *result = NULL;try{driver = sql::mysql::get_mysql_driver_instance();con = driver->connect("tcp://127.0.0.1:3306","root","000000");//连接数据库state = con->createStatement();state->execute("use wxf");//执行 选中wxf 这个数据库 命令(当然这个我有创建好,怎么创建网上查一下)result = state->executeQuery("select * from mytable");//查询 wxf.mytale 这张表中的所有字段}catch(sql::SQLException & ex)//如果上面有错就捕获这个异常{cout<<ex.what()<<endl;return;} while(result->next()){cout<<"name: "<<result->getString("name").c_str()<<endl;//取出字段为name的所有值}state->close();} void main(){RunConnectMySQL();getchar();;}

 


几句费话:

不知道为什么 SQLString 对象在VA的提示下会变成指针....  ,这样编译就会出错你把->改成就好了.

否则会报这类的错误:

__imp_?asStdString@SQLString@sql@@QBEABV?$basic_string@DV?$char_traits@D@stlp_std@@V?$allocator@D@2@@stlp_std@@XZ

mysql c++ 还貌似依赖boost //幸好我有装....

 

可能是久不写c++代码了 我非要写这样的代码

SQLString strsql = "sss";

string strstd = strql; //这里会报错,我还觉得的奇怪,同一个东西咋就不能哩.

看一下 SQLString 的定义:

class CPPCONN_PUBLIC_FUNC SQLString

      {

            std::string realStr;

     ...

         }

//它是个类... 并不是个宏,刚开始我一直把它看成是这个宏..... 类似POCO中的XMLString ... 看来是POCO用多了... 

 

 

http://blog.csdn.net/jgood/article/details/5661339

http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html#trx