Sqlite3 C Language API Simple Example

Source: Internet
Author: User

Function:

Simple C language using Sqlite3 API program
Attention: (1) In order to save space, the program does not write the code to handle the error
(2) Environment centOS6.5 system, database sqlite3, compiler GCC

/*file name: TEST.C interface: Open database: Sqlite3_open () Close database: Sqlite3_close () EXECUTE statement: sqlite3_exec () /c0>*/#include<stdio.h>#include<sqlite3.h>//sqlite3 API Library//callback function used in sqlite3_exec ()Static intCallback_selectall (void*,int,Char**,Char**);intMainvoid){    //used to store database names    Const Char* Db_name ="company.db"; //Open the database and create the database if it is notSqlite3 * pdb =NULL; Sqlite3_open (db_name,&pdb); //Create a tableSqlite3_exec (PDB,"CREATE TABLE person (ID integer primary key autoincrement, name char (+))", NULL, NULL, NULL); //inserting data into a tableSqlite3_exec (PDB,"Insert into person ( name) VALUES (\ "shang\")", NULL, NULL, NULL); Sqlite3_exec (PDB,"Insert into person ( name) VALUES (\ "guan\")", NULL, NULL, NULL); Sqlite3_exec (PDB,"Insert into person ( name) VALUES (\ "yuan\")", NULL, NULL, NULL); Sqlite3_exec (PDB,"Insert into person ( name) VALUES (\ "Xia\")", NULL, NULL, NULL); //Show all with select *Sqlite3_exec (PDB,"SELECT * from person", callback_selectall, NULL, NULL); //Close the databaseSqlite3_close (PDB); return 0;}//There are two parameters in the callback function that are not used//n_clumn Number of storage columns (ID, name) total two columns//column_value column values such as 1 shang,2 Guan//the other two parameters are not usedintCallback_selectall (void*params,intN_clumn,Char**column_value,Char**column_name) {    inti =0; //print values from a table     for(; i<n_clumn; i++) {printf ("%s", Column_value[i]); } printf ("\ n"); return 0;}

# makefile File
#-Lsqlite3 is a required option for compiling a linked database must=-lsqlite3-wall# dependent file src=test.c# compiled. Phony:test: -o test# virtual target. Phony:clean: -RF test# in terminal input make test to compile # Final build executable file test# at end user as. /Test View run result # in terminal input make clean cleanup unused test file

Run process

Sqlite3 C Language API Simple Example

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.