SQLite数据库使用like模糊查询中文问题

1.首先查看sqlite编码

PRAGMA encoding;

2.编写UTF-8与BG2312转换函数

//UTF-8到GB2312的转换
char* U2G(const char* utf8)
{
 int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
 wchar_t* wstr = new wchar_t[len+1];
 memset(wstr, 0, len+1);
 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
 len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
 char* str = new char[len+1];
 memset(str, 0, len+1);
 WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
 if(wstr) delete[] wstr;
 return str;
}

//GB2312到UTF-8的转换
char* G2U(const char* gb2312)
{
 int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
 wchar_t* wstr = new wchar_t[len+1];
 memset(wstr, 0, len+1);
 MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
 len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
 char* str = new char[len+1];
 memset(str, 0, len+1);
 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
 if(wstr) delete[] wstr;
 return str;
}

3.编写数据导入函数

bool AddData2LocalDb(CString strAdd)//strAdd为导入的数据
{
    CString strTemp;
    strTemp.Format(_T("INSERT INTO store VALUES(%s)"),strAdd);
    int nLength = strTemp.GetLength();
    int nBytes = WideCharToMultiByte(CP_ACP,0,strTemp,nLength,NULL,0,NULL,NULL);
    char *p = new char[ nBytes + 1];
    memset(p,0,nLength + 1);
    WideCharToMultiByte(CP_OEMCP, 0, strTemp, nLength, p, nBytes, NULL, NULL); 
    p[nBytes] = 0;

    p=G2U(p);
    if (sqlite3_exec (conn, p, NULL, NULL, &err_msg) != SQLITE_OK)
    {
        TRACE (_T("操作失败,错误代码:%s\n"), err_msg);
        strErr.Format(_T("操作失败,错误代码:%s"),err_msg);
        sqlite3_free(err_msg);
        return false;
    }
    return true;
}

4.编写like查询函数

strIn=输入的查询条件

strInfo=输出的查询到的数据

bool CtestmysqlDlg::LocalReadInfo_like(CString strIn,CString &strInfo)
{
    CString strTemp;
    strTemp.Format(_T("select *from Store where index1 like '%%%s%%'"),strIn);
    nLength = strTemp.GetLength();
    nBytes = WideCharToMultiByte(CP_ACP,0,strTemp,nLength,NULL,0,NULL,NULL);
    p = new char[ nBytes + 1];
    memset(p,0,nLength + 1);
    WideCharToMultiByte(CP_OEMCP, 0, strTemp, nLength, p, nBytes, NULL, NULL); 
    p[nBytes] = 0;
    stmt=NULL;
    p=G2U(p);
    if (SQLITE_OK!=sqlite3_prepare_v2(conn,p,strlen(p),&stmt,NULL))
    {
        if(stmt)
        {
            sqlite3_finalize(stmt);
        }
        sqlite3_close(conn);
        TRACE(_T("操作失败,错误代码:%s\n"), err_msg);
        sqlite3_free(err_msg);
        return false;
    }
    filedCount=sqlite3_column_count(stmt);
    do 
    {
        CString strText=_T("");
        int r=sqlite3_step(stmt);
        if(r==SQLITE_ROW)
        {
            for (int i=0;i<filedCount;i++)
            {
                const char* v = (const char*)sqlite3_column_text(stmt,i);
                v=U2G(v);
                CString strTemp0(v);
                if (filedCount==filedCount-1)
                {
                    strTemp0+=_T("&");
                }
                else
                {
                    strTemp0+=_T("$");
                }
                
                strText=strText+strTemp0;
            }
            strInfo=strInfo+strText;
            
        }
        else if (r == SQLITE_DONE) 
        {

            TRACE(_T("Select Finished.n"));

            break;

        } 
        else 
        {
            sqlite3_finalize(stmt);

            sqlite3_close(conn);

            return false;
        }

    }while (1);
    return true;
}

猜你喜欢

转载自blog.csdn.net/Hat_man_/article/details/108451160