The problem of database failure in SQLite Chinese path creation in C + +

Source: Internet
Author: User
Tags assert



Sqlite3 is used in the Utf-8 code, so when the database is created if the path is plain English letters and numbers, so many ansii and utf-8 encoding is the same, this time the Sqlite3_open function call completely no problem. However, if it is in Chinese, asnsii directly into the Utf-8 will be wrong, the path cannot be found, thus creating or opening the database failed. The workaround is as follows:


// The sdk can be directly converted from UNICODE to utf-8, not directly from ansii to utf-8
// So Ansii is converted to Utf-8, it needs to be converted to UNICODE first and then to utf-8
void UnicodeToUtf8 (char ** dest, const WCHAR * src)
 {
// ASSERT (dest! = NULL || src! = NULL);
     int len = -1;
     len = WideCharToMultiByte (CP_UTF8, 0, src, -1, 0, 0, 0, 0) +1;
     * dest = new char [len + 1];
     :: WideCharToMultiByte (CP_UTF8, 0, src, -1, * dest, len, 0, 0);
 }

 void AnsiToUtf8 (char ** dest, const char * src)
 {
    // ASSERT (dest! = NULL || src! = NULL);
     WCHAR * pwszStr = NULL;
     C2W (& pwszStr, src);
     UnicodeToUtf8 (dest, pwszStr);
     SAFE_ARRYDELETE (pwszStr);
 }

// Create \ Open database
       // szCreateTable = "c: \\ program files \\ yoyo \\ test.db"
        char * pszCreateSql = NULL;
     // Failed to create or open without this sentence
    AnsiToUtf8 (& pszCreateSql, szCreateTable);

    ///////////

    if (sqlite3_open (/ * szCreateTable * / pszCreateSql, ppdb)! = SQLITE_OK)
    {
        // Failed to open or create database
        sqlite3_close (* ppdb);
        * ppdb = null;
        return DB_FAILED;
    }
    else {} 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.