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,0,strlen);
strncpy(name, cstr1, strLength);
wchar_t* tmpname = m_strname.GetBuffer((m_strname.GetLength()));
name = UnicodeToAnsi(tmpname);
然后出现w_chart转为char的错误方法如下:
inline char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, 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,CP_UTF8),strlen(W2A_CP(name,CP_UTF8)),SQLITE_STATIC);