The use of the Sqlite3_open function of SQLite learning notes

Source: Internet
Author: User
Tags sqlite sqlite database

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 take the information on the Internet. Wrote one of the most frequently used functions from multibyte to UTF8, code such as the following:

Compilation environment: VS 2008+sp1,win7, Project is 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;}


Test code such as the following:

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 = MbcsToUt F8 (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;}

References:

1. Sqlite3 Problem 2: Chinese path support

The use of the Sqlite3_open function of SQLite learning notes

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.