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
Sqlite3 C Language API Simple Example