C ++ operate SQLite Databases

Source: Internet
Author: User
Tags dota
Before using C ++ to operate SQLite, You need to obtain sqlite3.h, sqlite3.lib, and sqlite3.dll. HereDownload. And import the three files into the VC ++ project. Put the sqlite3.dll file in the debug folder. SQLite API introduction int sqlite3_open (char * path, sqlite3 ** dB)
This function opens the database. The first parameter is the address of the SQLite file, and the second parameter is the pointer of sqlite3, that is, the second pointer. If the returned value is sqlite_ OK, the database is successfully opened.
Sqlite3_close (sqlite3 * dB)
This function closes the database. The parameter is a pointer to sqlite3.
Sqlite3_exec (sqlite3 * dB, char * SQL, int L, int M, int N)
This function executes the SQL statement. If we do not need the returned results, use this function to execute the SQL statement. The first parameter is the pointer of sqlite3, and the second parameter is the executed SQL statement. We don't need to care about the next three parameters, but set them to 0.
Sqlite3_get_table (SQLite * dB, char * SQL, char *** result, int * Row, int * column, int K );
This function executes the query statement and returns the required information. The first parameter is the pointer of SQLite, the second parameter is the SQL statement, and the third parameter is the returned information. Row is the number of returned rows, column is the number of returned columns, and the last parameter is set to 0.
Because we use gb2312 and SQLite uses UTF-8, Chinese garbled characters will appear during use. To solve this problem, I will introduce two useful functions for converting UTF-8 to gb3212.
char* U2G(const char* utf8){ int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); wchar_t* wstr = new 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 = new char[len+1]; memset(str, 0, len+1); WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL); if(wstr) delete[] wstr; return str;}

Conversion from gb2312 to UTF-8

char* G2U(const char* gb2312){ int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0); wchar_t* wstr = new 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 = new char[len+1]; memset(str, 0, len+1); WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); if(wstr) delete[] wstr; return str;}

These two functions will be used. We need to introduce the windows. h header file. A practical project I have done is in my project. I encapsulated the API for ease of operation. I created a header file called sqlitehelper class, which is as follows:

#if !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)#define AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "sqlite3.h"#include <windows.h>class SQLiteHelper  {public:SQLiteHelper();virtual ~SQLiteHelper();sqlite3 *db;void execSQL(char *sql);char**rawQuery(char *sql,int *row,int *column,char **result);void openDB(char *path);void closeDB();};#endif // !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)


The source file is as follows:

# Include "sqlitehelper. H "# include <iostream. h> ////////////////////////////////////// /// // construction/destruction /// //////////////////////////////////////// /// // sqlitehelper:: sqlitehelper () {} sqlitehelper ::~ Sqlitehelper () {} void sqlitehelper: execsql (char * SQL) {sqlite3_exec (dB, SQL, 0, 0);} Char ** sqlitehelper: rawquery (char * SQL, int * Row, int * column, char ** result) {sqlite3_get_table (dB, SQL, & result, row, column, 0); return result;} void sqlitehelper :: opendb (char * path) {int last = sqlite3_open (path, & dB); If (sqlite_ OK! = Last) {cout <"An error occurred while opening the Database" <Endl; return; postquitmessage (0) ;}} void sqlitehelper: closedb () {sqlite3_close (db );}


My main function classes are as follows:

Include <iostream. h> # include <windows. h> # include "sqlite3.h" # include "sqlitehelper. H "# pragma comment (Lib," sqlite3.lib ") // convert UTF-8 to gb3212char * u2g (const char * utf8) {int Len = multibytetowidechar (cp_utf8, 0, utf8, -1, null, 0); wchar_t * wstr = new 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 = new char [Len + 1]; memset (STR, 0, Len + 1); widechartomultibyte (cp_acp, 0, wstr,-1, STR, Len, null, null); If (wstr) Delete [] wstr; return STR;} // convert from gb2312 to UTF-8 char * g2u (const char * gb2312) {int Len = multibytetowidechar (cp_acp, 0, gb2312,-1, null, 0); wchar_t * wstr = new 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 = new char [Len + 1]; memset (STR, 0, Len + 1); widechartomultibyte (cp_utf8, 0, wstr,-1, STR, Len, null, null); If (wstr) Delete [] wstr; return STR;} void main () {sqlitehelper * Help = new sqlitehelper (); Help-> opendb ("D: \ zhycheng. db3 "); char * SQL =" insert into Dota values (6, 'zhycheng') "; Help-> execsql (SQL ); char * sql2 = "select * From Dota"; int row, Col; char * EEE = "I"; char ** result = & EEE; char ** Re = Help-> rawquery (sql2, & Row, & Col, result); char * LL = u2g (Re [(2 + 1) * Col + 1]); cout <ll <Endl; Help-> closedb ();}

Here I will explain that re [(2 + 1) * Col + 1] Re is a pointer to an array. (2 + 1) is 3rd rows, and 1 indicates 2nd columns.
As you can see, I read the string "Zhang translated. Note that when writing data, if you want to write data into Chinese, you need to convert the Chinese from gb2312 to UTF-8 and then write the data. According to your project requirements, I have provided the function.






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.