Zhu Jinchan
Source: http://blog.csdn.net/clever101
The Sqlite3_open function is required to open a SQLite database, but the first parameter of the Sqlite3_open function is the absolute path to the database file. It is fastidious and must be a UTF8 string. That is, if the file path is a non-UTF8 character, it is converted to UTF characters. So referring to the information on the Internet, wrote a most commonly used from multi-byte to UTF8 function, the code is as follows:
Compilation environment: VS 2008+sp1,win7, Project set to multibyte character set std::string MbcsToUtf8 (const char* PSZMBCS) {std::string str; WCHAR *pwchar=0; CHAR *pchar=0;int Len=0;int codepage = Arefileapisansi ()? Cp_acp:cp_oemcp;len=multibytetowidechar (codepage, 0, Pszmbcs,-1, null,0);p wchar=new wchar[len];if (pwchar!=0) {len = MultiByteToWideChar (codepage, 0, Pszmbcs,-1, Pwchar, Len); if (len!=0) {len = WideCharToMultiByte (Cp_utf8, 0, Pwchar,-1, 0, 0, 0, 0);p char=new char[len];if (pchar!=0) {len = WideCharToMultiByte (Cp_utf8, 0, Pwchar,-1, Pchar, len,0, 0); if (len!=0) {str = pchar; } Delete Pchar;} Delete Pwchar;}} return str;}
The test code is as follows:
Build environment: VS 2008+sp1,win7, Project set to multibyte character Set # include <sqlite3.h> #include <string>int main (void) {sqlite3* db = 0; char* pszerrmsg = null;//Connection Database std::string strimgindexfilename = "C:\\test.db" std::string strutf8file = MbcsToUtf8 ( Strimgindexfilename.c_str ()); int ret = Sqlite3_open (Strutf8file.c_str (), &db); if (ret! = SQLITE_OK) {fprintf ( stderr, "Unable to open database:%s", sqlite3_errmsg (db)); return 1;} return 0;}
Reference documents:
1. Sqlite3 Problem 2: Chinese path support
The use of the Sqlite3_open function of SQLite learning notes