sqlite3 中由于是开源的基于c/c++的库,所以参数为char类型,但由于MFC中,控件变量多为使用Cstring,所以之间会存在字符类型强制转换最常用的方法:
1:sqlite3保持参数为char类型,把MFC中cstring强制转换为char,方法如下:::
int strlen = m_strname.GetLength()+1;
char *name = new char[strlen];
memset(name,strlen);
strncpy(name,cstr1,strLength);
wchar_t* tmpname = m_strname.GetBuffer((m_strname.GetLength()));
name = UnicodeToAnsi(tmpname);
inline char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP,szStr,-1,NULL,NULL );
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP,pResult,nLen,NULL );
return pResult;
}
2:在sqlite3中使用宏转换,自己写的函数参数任为cstring,方法如下:
USES_CONVERSION;////////必须加这个,如果不加会包_lps未定义 int rc= sqlite3_prepare_v2(db,W2A_CP(sqlstr,CP_UTF8),strlen(W2A_CP(sqlstr,CP_UTF8)),&stmt,NULL); /*char *c_name = (LPSTR)(LPCTSTR)name;*/ rc =sqlite3_bind_text(stmt,1,W2A_CP(name,strlen(W2A_CP(name,sqlITE_STATIC);
原文链接:https://www.f2er.com/sqlite/199799.html