Copy Code code as follows:
#include <stdio.h>
#include <sqlite3.h>
int select_callback (void *data,int Col_count,char **col_values,char **col_name)
{
Each record recalls the function once, how many times it is recalled
int i;
for (i=0;i<col_count;i++)
{
printf ("%s=%s\n", col_name[i],col_values[i]==0?) NULL ": Col_values[i]);
}
return 0;
}
int main (int argc,char **argv)
{
Create a database table
const char *sql1= "CREATE TABLE users (ID int PRIMARY key,name varchar (), birthday datetime);";
Inserting data into the database
const char *sql2= "INSERT into users values (1, ' myd ', ' 2013-10-9 ');";
const char *sql3= "INSERT into users values (2, ' myd ', ' 2013-10-9 ');";
Querying data from a database
const char *sql4= "SELECT * from users;";
Char *errmsg=0;
int ret = 0;
Connecting to a database
Sqlite3 *db = 0;
Ret=sqlite3_open ("./database", &db);
if (ret!= SQLITE_OK)
{
fprintf (stderr, "Cannot Open database:%s", sqlite3_errmsg (db));
return 1;
}
printf ("Database connection succeeded!") \ n ");
Perform a Build table
ret = sqlite3_exec (db,sql1,0,0,&errmsg);
if (ret!= SQLITE_OK)
{
fprintf (stderr, "SQL error:%s\n", errmsg);
Sqlite3_free (errmsg);
}
Execute INSERT Record SQL statement
ret = sqlite3_exec (db,sql2,0,0,&errmsg);
if (ret!=SQLITE_OK)
{
printf ("Insert data succeeded \ n");
}
ret = sqlite3_exec (db,sql3,0,0,&errmsg);
{
printf ("Insert data succeeded \ n");
}
Querying data table Contents
printf ("Query data table content \ n");
Sqlite3_exec (DB,SQL4,SELECT_CALLBACK,0,&ERRMSG);
Close Database
Sqlite3_close (DB);
db = 0;
printf ("Database shutdown succeeded!") \ n ");
return 0;
}
Copy Code code as follows:
Cflags=-l/usr/local/sqlite-autoconf-3070400/lib-i/usr/local/sqlite-autoconf-307040/include-lsqlite3
ALL:SQLITE.O SQLite
Sqlite:sqlite.o
GCC sqlite.o-o SQLite $ (cflags)
Sqlite.o:sqlite.c
Gcc-c sqlite.c $ (cflags)
Clean
RM-RF SQLite *.O