测试mysql二进制字段长度_测试mysql++的多字符集及二进制字段

/// linux version

/

#include

#include

#include

#include

using namespace std;

using namespace mysqlpp;

int testmysqlpp( )

{

iconv_t igu = iconv_open("utf8", "gb2312");

iconv_t iug = iconv_open("gb2312",

"utf8");

mysqlpp::Connection conn(false);

Option* pOption = new

SetCharsetNameOption("gb2312");

bool bRet = conn.set_option(pOption);

if( conn.connect("example", "192.168.1.106",

"root", "123456"))

{

Query query =

conn.query("select username, password, role, refcount from

t_user");

if( StoreQueryResult res =

query.store() )

{

mysqlpp::StoreQueryResult::const_iterator

it;

for(it =

res.begin() ; it != res.end(); it++ )

{

Row

row = *it;

char

achUsername[512] = {0};

size_t

iUsernameLen = 512;

char

achInput[512] = {0};

size_t

nFieldLen = row[0].length();

strncpy(achInput,

(char*)row[0].c_str(), nFieldLen);

char*

pInput = achInput;

char*

pOutput = achUsername;

size_t

nRet = iconv(igu, (const char**)&pInput,

&nFieldLen, &pOutput,

&iUsernameLen);

cout<<

'\t' << achUsername

<< '\t'

<< row[1]

<< '\t'

<< row[2]

<< '\t'

<< row[3]

<<

endl; }

}

query.reset();

char* pchUser = "中国";

size_t nUserLen =

strlen(pchUser);

char achUser[512] =

{0};

size_t nUserBufLen = 512;

char* pUser = achUser;

size_t nRet = iconv(iug, (const

char**)&pchUser, &nUserLen,

&pUser, &nUserBufLen);

string strInsert = "insert into

t_user (username, password,role,refcount) values('";

strInsert += achUser;

strInsert +=

"','9dd4e12',1,0)";

String str(strInsert);

query

<< str;

bool ret = query.exec();

iconv_close(igu);

iconv_close(iug);

Query blobQuery =

conn.query("select * from t_img");

if( StoreQueryResult result =

blobQuery.store() )

{

StoreQueryResult::const_iterator

it;

for(it =

result.begin() ; it != result.end(); it++ )

{

Row

row = *it;

sql_blob

img = row[1];

int

len = img.length();

char*

pImg = new char[len];

memcpy(pImg,

img.data(),

len); }

}

unsigned char achImg[16] =

{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21,

0x22, 0x23, 0x24, 0x25, 0x26};

string strBin;

strBin.assign((char*)achImg,

16);

Query binquery =

conn.query();

binquery

<< "insert into t_img(id, image)

values(2,\"" << mysqlpp::escape

<< strBin

<< "\")";

bool bBinRet =

binquery.exec();

return 0;

}

return 0;

}

/// windows version

#include "cmdline.h"

#include "printdata.h"

#include

#include

#include

#include

using namespace std;

#ifdef WIN32

static inline char *FromWide (const wchar_t *wide, char* pOut, int

nOutLen)

{

size_t len = WideCharToMultiByte (CP_UTF8, 0,

wide, -1, NULL, 0, NULL, NULL);

if (len == 0)

return NULL;

if (pOut)

WideCharToMultiByte (CP_UTF8,

0, wide, -1, pOut, nOutLen, NULL, NULL);

return pOut;

}

static inline wchar_t *ToWide (const char *utf8, wchar_t * pOut,

int nOutLen)

{

size_t len = MultiByteToWideChar (CP_UTF8, 0,

utf8, -1, NULL, 0);

if (len == 0)

return NULL;

if (pOut)

MultiByteToWideChar (CP_UTF8,

0, utf8, -1, pOut, nOutLen);

return pOut;

}

int MultiByteToWideChar1(LPCSTR lpcSrcStr, LPWSTR lpwDstStr, int

nDstSize)

{

int size = MultiByteToWideChar(CP_ACP, 0,

lpcSrcStr, -1, NULL, 0);

if( nDstSize > size )

{

return

MultiByteToWideChar(CP_ACP, 0, lpcSrcStr, -1, lpwDstStr,

size);

}

return 0;

}

int WideCharToMultiByte1(LPWSTR lpwSrcStr, LPSTR lpcDstStr, int

nSize)

{

int size = WideCharToMultiByte(CP_OEMCP, NULL,

lpwSrcStr, -1, NULL, 0, NULL, FALSE);

if( nSize > size)

{

return

WideCharToMultiByte(CP_OEMCP, NULL, lpwSrcStr, -1, lpcDstStr,

nSize, NULL, FALSE);

}

return 0;

}

#endif

int

main(int argc, char *argv[])

{

// Get database access parameters from command

line

mysqlpp::examples::CommandLine cmdline(argc,

argv);

if (!cmdline) {

return 1;

}

// Connect to the sample database.

mysqlpp::Connection conn(false);

if (conn.connect(mysqlpp::examples::db_name,

cmdline.server(),

cmdline.user(),

cmdline.pass())) {

// Retrieve a subset of the

sample stock table set up by resetdb

// and display it.

mysqlpp::Query query =

conn.query("select item from stock");

if (mysqlpp::StoreQueryResult

res = query.store()) {

cout

<< "We have:"

<< endl;

mysqlpp::StoreQueryResult::const_iterator

it;

for (it =

res.begin(); it != res.end(); ++it) {

mysqlpp::Row

row = *it;

cout

<< '\t'

<< row[0]

<< endl;

}

}

else {

cerr

<< "Failed to get item list: "

<< query.error()

<< endl;

return

1;

}

conn.disconnect();

conn.connect("example",

"192.168.1.106", "root", "123456");

query.reset();

query = conn.query("select

username, password, role, refcount from t_user");

if( mysqlpp::StoreQueryResult

res = query.store() )

{

mysqlpp::StoreQueryResult::const_iterator

it;

for(it =

res.begin() ; it != res.end(); it++ )

{

mysqlpp::Row

row = *it;

char

achUsername[512] = {0};

size_t

iUsernameLen = 512;

char

achInput[512] = {0};

size_t

nFieldLen = row[0].length();

strncpy(achInput,

(char*)row[0].c_str(), nFieldLen);

TCHAR

user[512] = {0};

ToWide(achInput,

user, 511);

WideCharToMultiByte1(user,

achUsername, 511);

cout<<

'\t' << achUsername

<< '\t'

<< row[1]

<< '\t'

<< row[2]

<< '\t'

<< row[3]

<<

endl; }

}

query.reset();

TCHAR* pchUser =

_T("中国");

size_t nUserLen =

_tcslen(pchUser);

char achUser[512] =

{0};

size_t nUserBufLen = 512;

char* pUser = achUser;

FromWide(pchUser, achUser,

511);

string strInsert = "insert

into t_user (username, password,role,refcount) values('";

strInsert += achUser;

strInsert +=

"','9dd4e12',1,0)";

mysqlpp::String

str(strInsert);

query

<< str;

bool ret = query.exec();

unsigned char achImg[16] =

{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21,

0x22, 0x23, 0x24, 0x25, 0x26};

string strBin;

strBin.assign((char*)achImg,

16);

mysqlpp::Query binquery =

conn.query();

binquery

<< "insert into t_img(id, image)

values(2,\"" << mysqlpp::escape

<< strBin

<< "\")";

bool bBinRet =

binquery.exec();

return 0;

}

else {

cerr

<< "DB connection failed: "

<< conn.error()

<< endl;

return 1;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值