#include
#include
#include"/usr/local/mysql/include/mysql/mysql.h"
using namespace std;
struct AA
{
int a;
char c[100];
int b;
};
int main()
{
MYSQL *mysql = mysql_init(NULL);
if(!mysql)
{
cout<<"mysql_init error"< }
if(!mysql_real_connect(mysql,"192.168.57.192","root","123456","test",3306,NULL,0))
{
cout<<"mysql_real_connect"< }
char* sql ="select bb from myblob";
int ret = mysql_real_query(mysql,sql,strlen(sql));
if (ret != 0)
{
cout<<"mysql_real_query:"< }
MYSQL_RES *res = NULL;
MYSQL_ROW row;
res = mysql_store_result(mysql);
if (NULL == res)
{
cout<<"mysql_store_result"<
}
int nFieldCount = mysql_num_fields(res);
int nRowCount = mysql_num_rows(res);
unsigned long *len;
for (uint32_t i = 0; i < nRowCount; ++i)
{
row = mysql_fetch_row(res);
len= mysql_fetch_lengths(res);
for (uint32_t j = 0; j < nFieldCount; ++j)
{
AA a;
memcpy(&a,row[j],len[j]);
cout<<"a:"<
/*
cout<<"len:"< cout<<((AA*)row[j])->a<<" ";
cout<<((AA*)row[j])->c<<" ";
cout<<((AA*)row[j])->b<<" ";
*/
}
cout< }
MYSQL_STMT *stmt = mysql_stmt_init(mysql);
if(!stmt)
{
cout<<"mysql_stmt_init"< }
string s = "insert into `myblob` values(NULL,?)";
if(mysql_stmt_prepare(stmt,s.c_str(),s.size()))
{
cout<<"mysql_stmt_prepare"< cout<<"prepare:"< }
MYSQL_BIND bd[1];
unsigned long length = 0;
AA aa;
aa.a = 10;
char* ss = "hahaha锛岃繖鏄?;
strcpy(aa.c,ss);
aa.b = 20;
off_t size;
memset(bd,0,sizeof(bd));
bd[0].buffer = (char*)&aa;
bd[0].buffer_type = MYSQL_TYPE_BLOB;
bd[0].length = &length;
bd[0].is_null = 0;
if(mysql_stmt_bind_param(stmt,bd))
{
cout<<"mysql_stmt_bind_param"< }
int rc = 0;
if(mysql_stmt_send_long_data(stmt,0,(char*)&aa,sizeof(aa)))
{
cout<<"mysql_stmt_send_long_data"< }
if(mysql_stmt_execute(stmt))
{
cout<<"mysql_stmt_execte"< }
return 0;
}
阅读(1533) | 评论(0) | 转发(0) |