2014-01-25 39 views

回答

1

當您使用sqlite3_open16功能打開DB與UTF-16編碼。 當您使用sqlite3_exec(m_db,"UPDATE t1 SET a1='текст'",0,0,0)函數時,您使用編譯器的編碼(默認爲UTF-8或cp1251)。

您應該在兩種情況下使用相同的編碼。您可以執行以下其中一項操作:

1)使用sqlite3_open函數以UTF-8編碼打開它,並確保您的編譯器使用UTF-8;

2)將您的編譯器編碼更改爲UTF-16;

3)使用SQL語句PRAGMA encoding = "UTF-8"

+0

非常感謝! –

1
string ANSItoUTF16le(const char* v_str) 
{ 
    string t_res; 
    unsigned char* t_buf=(unsigned char*)v_str; 
    auto t_size=strlen(v_str); 
    unsigned char t_s='А',t_f='п',t_E='Ё',t_e='Ё',t2_s='р',t2_f='я'; 
    unsigned char t_c; 
    for(auto i=0;i<t_size;i++) 
    { 
     if(t_buf[i]>=t_s&&t_buf[i]<=t_f) 
     { 
      t_res+='\xD0'; 
      t_c='\x90'+t_buf[i]-t_s; 
      t_res+=t_c; 
      continue; 
     } 
     if(t_buf[i]>=t2_s&&t_buf[i]<=t2_f) 
     { 
      t_res+='\xD1'; 
      t_c='\x80'+t_buf[i]-t2_s; 
      t_res+=t_c; 
      continue; 
     } 
     if(t_buf[i]==t_E) 
     { 
      t_res+="\xD0\x01"; 
      continue; 
     } 
     if(t_buf[i]==t_e) 
     { 
      t_res+="\xD1\x91"; 
      continue; 
     } 
     t_res+=v_str[i]; 
    } 
    return t_res; 
} 

sqlite3_exec(m_db,ANSItoUTF16le("UPDATE t1 SET a1='текст'").c_str(),0,0,0); 
+0

謝謝安德烈! –

0

您可以通過使用sqlite3的頂部的包裝類插入Unicode字符串到sqllite在C++中。

CppSqlite3U是一個圍繞SQLite數據庫的unicode包裝。它允許使用UNICODE類型,如wchar_t(而不是char),或者可以從here下載。也在this article中描述。例如:由execQuery執行的查詢可以是LPCTSTR類型,這意味着它可以是一個寬字符或字符的數組,取決於UNICODE是否在您的項目中定義。

CppSQLite3Query execQuery(LPCTSTR szSQL);