Example of C + + invocation for lightweight data SQLite

Source: Internet
Author: User

Original address: http://www.cnblogs.com/kfqcome/archive/2011/06/27/2136999.html

#include"stdafx.h"#include"sqlite3.h"Static intCallbackvoid*notused,intargcChar**ARGV,Char**azcolname) {    inti;  for(i=0; i<argc; i++) {printf ("%s =%s/n", Azcolname[i], argv[i]? Argv[i]:"NULL"); } printf ("/ N"); return 0;}#defineCHECK_RC (RC,SZINFO,SZERRMSG,DB) if (RC!=SQLITE_OK) \{printf ("%s error!/n", Szinfo); printf ("%s/n", szerrmsg);           Sqlite3_free (SZERRMSG);           Sqlite3_close (DB); return 0;}int_tmain (intARGC, _tchar*argv[]) {Sqlite3*DB; Char*dbpath="f:/test.db"; Char*szerrmsg =0; intRc= Sqlite3_open (DbPath, &db); CHECK_RC (RC,"Open Database", DB); Char*szsql="CREATE TABLE UserInfo (ID int primary key, UserName Char, PassWord char);"; RC=sqlite3_exec (Db,szsql,0,0,&szerrmsg); CHECK_RC (RC,"CREATE TABLE", szerrmsg,db); RC=sqlite3_exec (DB,"INSERT INTO UserInfo (Id,username,password) VALUES (1, ' kfqcome ', ' 123456 ')",0,0,&szerrmsg); CHECK_RC (RC,"Insert Info", szerrmsg,db); RC=sqlite3_exec (DB,"INSERT INTO UserInfo (Id,username,password) VALUES (2, ' Miss Wang ', ' 654321 ')",0,0,&szerrmsg); CHECK_RC (RC,"Insert Info", szerrmsg,db); szSQL="SELECT * from UserInfo"; RC= Sqlite3_exec (Db,szsql, Callback,0, &szerrmsg); CHECK_RC (RC,"Query Values", szerrmsg,db);    Sqlite3_close (DB);    GetChar (); return 0;}

Results of the output:

ID = 1

UserName = Kfqcome

PassWord = 123456

ID = 2

UserName = Miss Wang

PassWord = 654321

The execution of the SQL statement here is sqlite3_exec, which is the encapsulation of the previous functions

int Sqlite3_exec (

sqlite3*,/* An open database */

const char *SQL,/* SQL to be evaluated */

Int (*callback) (void*,int,char**,char**),/* Callback function */

void *,/* 1st argument to callback */

Char **errmsg/* Error msg written here */

);

Sqlite3_exec is the encapsulation of Sqlite3_prepare_v2,sqlite3_step () and Sqlite3_finalize (), which allows the program to execute SQL statements multiple times without writing many repetitive code.

The Sqlite3_exec interface executes 0 or more UTF-8 encoded, semicolon-delimited SQL statements that are passed to the second parameter. If Sqlite3_exec's third parameter callback function pointer is not NULL, then it will be called for each result row from the executed SQL statement (that is, the callback function will be called multiple times, the above example will return 2 result rows, which will be executed 2 times), and the 4th parameter is the first parameter passed to the callback function , if the callback function pointer is empty, then the callback does not occur and the resulting row is ignored.

If an error occurs in the execution of the SQL statement, the execution of the current statement is stopped and subsequent statements are skipped. When the fifth parameter is not empty, it is allocated memory and writes an error message, so you need to call Sqlite3_free to release the object after sqlite3_exec to prevent memory leaks

callback function:

Int (*callback) (void*,int,char**,char**),/* Callback function */

The first parameter is passed in by the fourth parameter of the sqlite3_exec.

The second parameter is the number of columns in the result row

The third parameter is a pointer to the column data in a row

The fourth parameter is a pointer to the column name in the row

Example of C + + invocation for lightweight data SQLite

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.