SQLite3 Simple Primer and C + + API

Source: Internet
Author: User
Tags sql error sqlite

Reprint Please specify source: http://www.cnblogs.com/StartoverX/p/4660487.html

The project is used to SQLite3, a simple record.

Unlike MySQL, the SQLite3 database is based on file storage, does not need to communicate with the server process, and reads the database content directly through the API call. db files.

1. Installation

Direct Apt-get under Ubuntu: sudo apt-get install sqlite3 Libsqite3-dev

2. Data type (DataType)

SQLite3 have v basic types, namely NULL, INTEGER, REAL, TEXT, BLOB.

Null: null value.

Integer: Signed integer, stored in 1,2,3,4,6 or 8 bytes Depending on the size of the value

Real:float real types, stored in 8-byte IEEE floating-point numbers.

Text: The value is a literal string, stored using the database encoding (UTF-8,utf-16be, or utf-16le).

BLOB: Just a block of data that is stored exactly as input (that is, no change), memory chips, audio and video, etc.

3. Create a Database

The shell enters the directory that needs to create the database file, directly sqlite3 test.db can, query databases,tables with. Databases,.tables, view help with.

4.c++ API

 

The above is the most basic sqlite3 C + + API, and the most commonly used is Sqlite3_open (), Sqlite3_exec (), Sqlite3_close (). Sqlite3_exec () is an encapsulation of sqlite3_prepare (), Sqlite3_step (), Sqlite3_column (), and Sqlite3_finalize (), which completes the execution and return of an SQL statement. Examples are as follows:

Note that callback is a callback function for Sqlite3_exec (), and the third parameter of Sqlite3_exec () is passed into the first parameter of callback, thus enabling communication with the context.
 on#include <stdio.h> Geneva#include <sqlite3.h>Geneva  Geneva  Static intCallbackvoid*arg,intargcChar**ARGV,Char**azcolname) { to    inti; .     for(i=0; i<argc; i++){ -printf"%s =%s\n", Azcolname[i], argv[i]? Argv[i]:"NULL"); ,    } theprintf"\ n");Ten    return 0; One  } A   -  intMainintargcChar**argv) { -Sqlite3 *db; the    Char*zerrmsg =0; -    intRC; -   -    if(argc!=3 ){ +fprintf (stderr,"Usage:%s DATABASE sql-statement\n", argv[0]); -      return(1); +    } Arc = Sqlite3_open (argv[1], &db); at    if(RC) { -fprintf (stderr,"Can ' t Open database:%s\n", sqlite3_errmsg (db)); -sqlite3_close (db); -      return(1); -    } -rc = sqlite3_exec (db, argv[2], Callback,0, &zerrmsg); in    if(rc!=SQLITE_OK) { -fprintf (stderr,"SQL Error:%s\n", zerrmsg); toSqlite3_free (zerrmsg); +    } -sqlite3_close (db); the    return 0; *}

Reference: An Introduction to the SQLite C/C + + Interface

SQLite in 5 Minutes or less

SQLite3 Simple Primer and C + + API

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.