O banco de dados SQLite usa como um problema chinês de consulta difusa

1. Primeiro verifique a codificação sqlite

Codificação PRAGMA;

2. Escreva a função de conversão UTF-8 e BG2312

// UTF-8 到 GB2312 的 转换
char * U2G (const char * utf8)
{  int len ​​= MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);  wchar_t * wstr = novo 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 = novo 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 = novo 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 = novo 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. Gravar função de importação de dados

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 = novo 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 ("A operação falhou, código de erro:% s \ n"), err_msg);         strErr. Format (_T ("A operação falhou, código de erro:% s"), err_msg);         sqlite3_free (err_msg);         retornar falso;     }     retornar verdadeiro; }






4. Escreva como função de consulta

strIn = condições de consulta de entrada

strInfo = dados de consulta de saída

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 = novo 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 (conexão);
        TRACE (_T ("操作 失败 , 错误 代码 :% s \ n"), err_msg);
        sqlite3_free (err_msg);
        retorna falso;
    }
    filedCount = sqlite3_column_count (stmt);
    faça 
    {         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"));

            quebrar;

        } 
        else 
        {             sqlite3_finalize (stmt);

            sqlite3_close (conexão);

            retorna falso;
        }

    } enquanto (1);
    return true;
}

Acho que você gosta

Origin blog.csdn.net/Hat_man_/article/details/108451160
Recomendado
Clasificación