CppSQLite3U usage summary, cppsqlite3u Summary

Source: Internet
Author: User

CppSQLite3U usage summary, cppsqlite3u Summary

CppSQLite3U is a class for operating sqlite3 that can be used by encapsulated MFC. I wrote a copy of it myself, but later I always felt that it was not mature enough to modify the code and felt that it was not perfect, I simply found this on the internet and used it for a while. After that, I found that it was quite good. duang's performance came out, and now I am writing this article, I want to tell you that it is easy and convenient to use. After you finish reading my introduction, you can also.

The attachment will be uploaded later. There will be a total of five files

CppSQLite3U. cpp

CppSQLite3U. h

Sqlite3.h

Sqlite3.lib

Sqlite3.dll

Put. h and. cpp files are added to the project ,. lib. dll is placed in the project root directory, and sqlite3.lib is written into Object/libiary modules in the project setting link to introduce this item. Of course it is the same in the code, but I forgot how to write it, haha, what should I do if I have tools?

Okay, this is the initial loading method. Next, let's take a look at how to use it. I only recorded what I used, because there are a lot of online materials and detailed descriptions, but I didn't use that much, I just want to write a simple localization tool, so I only want to record the simple usage, in order to make it easy for me to quickly check it during later use. for work reasons, it's not very easy to use mfc. I 've forgotten it a few months ago and can only record it in this form.

1. include CppSQLite3U. h In the source code to be referenced. It seems nonsense, but I have forgotten to write the code like this.

2. cppsqlitedb; // Declaration

3. db. open ("database name. db") // sqlite database file name

4. CppSQLite3Query query;

5. query = db.exe cQuery ("SQL statement ");

6. for (int I = 0; I <query. numFields (); ++ I) {// numFields () indicates the total number of columns, that is, the total number of fields.

Query. fieldName (I); // fieldName (I) indicates the column name, that is, the field name. You can use the price AS amount in the SQL statement. In this way, the field name is used, which is convenient.

}

7. while (! Query. eof ()){

Query. eof (); // when it is true, it indicates that there is no data after the header

Query. getStringField (); // you can enter the column number or field name to obtain the field value.

Query. nextRow (); // jump to the next data row

}

8. query. finalize (); // release the Query

9. db. close (); // close the database;

Note that the data obtained after the query, that is, getStringField ();, is utf8 encoded, because I use vc6 and vc6 is ansi by default, so for garbled characters, it is necessary to try transcoding before using it. I wrote a Convert function, but still some minor problems have not been solved, but it does not affect the use, this function also saves you trouble finding it later. It is actually found online ,--!

/* Usage: Convert (strA_in, strB_out, CP_UTF8, CP_ACP) // UTF8 converts ANSIConvert (strA_out, strB_in, CP_ACP, CP_UTF8) // ANSI Convert UTF8 */void Convert (const char * strIn, char * strOut, int sourceCodepage, int targetCodepage) {int len = lstrlen (strIn ); int unicodeLen = MultiByteToWideChar (sourceCodepage, 0, strIn,-1, NULL, 0); wchar_t * pUnicode; pUnicode = new wchar_t [unicodeLen + 1]; memset (pUnicode, 0, (unicodeLen + 1) * sizeof (wchar_t); MultiByteToWideChar (sourceCodepage, 0, strIn,-1, (LPWSTR) pUnicode, unicodeLen); BYTE * pTargetData = NULL; int targetLen = WideCharToMultiByte (targetCodepage, 0, (LPWSTR) pUnicode,-1, (char *) pTargetData, 0, NULL, NULL); pTargetData = new BYTE [targetLen + 1]; memset (pTargetData, 0, targetLen + 1); WideCharToMultiByte (targetCodepage, 0, (LPWSTR) pUnicode,-1, (char *) pTargetData, targetLen, NULL, NULL ); lstrcpy (strOut, (char *) pTargetData); delete pUnicode; delete pTargetData ;}

The above section describes how to traverse and query the data in the database, and then describes how to insert the data:

Db.exe cDML ("SQL statement ");

There is also a function, also write, query. getIntFiel ("NUM"); obtains an integer value based on the field name. Here, NUM is obtained through the query statement in the previous step: query = db.exe cQuery ("select count (*) as num from tableName ")

An exception occurs:

Try {

} Catch (CppSQLite3Exception e ){

MessageBox (e. errorMessage ());

}

In addition, sqlite3 does not have the enum type. Therefore, if necessary, create another table to store the type.

/*
Create table if not exists PriceType (
Type VARCHAR (10) primary key not null,
Seq INTEGER UNIQUE
);
Insert into PriceType (type, seq) VALUES ('overput', 1 );
Insert into PriceType (type, seq) VALUES ('revenue ', 2 );
Insert into PriceType (type, seq) VALUES ('dinner ', 3 );
Insert into PriceType (type, seq) VALUES ('rent ', 4 );
Insert into PriceType (type, seq) VALUES ('repaying loan', 5 );
Insert into PriceType (type, seq) VALUES ('smoking ', 6 );
*/


The basic usage is recorded here temporarily. If there is a new learning record later


Attachment address: http://download.csdn.net/detail/qq88468560/8535969

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.