VC2010利用MySQL++访问mysql. 及连接池示例

参加这个哥们的博客

http://blog.csdn.net/fcuandy/article/details/6728263

我的一个测试例子,用到了blob;

#include<string>
#include<iostream>
#include<vector>
#include<mysql++.h>
#include <ssqls.h>
using namespace mysqlpp;
using namespace std;

sql_create_3(sorttest,1,3,sql_int_unsigned,id,sql_char,name,sql_blob,bin)

struct node
{
unsigned int a;
unsigned int b;
node(){a=0;b=0;}
};
int main()
{
try
{
Connection con(true);
con.connect("mysqltest","localhost","xxx","xxxxxx",3306);  //up to u
cout<<"connect to server ok";
Query query =con.query();
// if (StoreQueryResult res = query.store())
// {
// cout<<"the it follow:"<<endl;
// for (size_t i=0; i<res.num_rows(); ++i)
// cout<<res.types(i).name<<"\t"<<res[i]["sex"]<<"\t"<<res[i]["sex"]<<endl;
// }
sql_blob str;
  struct node td;
  td.a=801;
  td.b=580;
  str.assign((char*)&td,sizeof(td));
  sorttest temp(4,"go",str);
  cout<<temp.id<<temp.name<<temp.bin.c_str();
  query.insert(temp);
  cout<<query;
  query.execute();

vector<sorttest> v;
query << "select * from sorttest";  //your table name
query.storein(v);
for (vector<sorttest>::iterator it=v.begin(); it!=v.end(); ++it)
{
cout<<it->id<<"\t"<<it->name<<"\t";
struct node rd;
const char *p = it->bin.data();
rd.a = (*(unsigned int *)&p[0]);
rd.b = (*(unsigned int *)&p[4]);
cout<<rd.a<<"\t"<<rd.b<<endl;
}

cin.get();
con.disconnect();
return 0;
}
catch(BadQuery   er) 

cout<<"erronr"<<endl; 
cin.get();
return 0; 


}




你可能感兴趣的:(sql,mysql,struct,iterator,query,2010)