Linux C Language Operations database (connection SQLite database) _c language

Source: Internet
Author: User
Tags sql error sqlite sqlite database

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,&AMP;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

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.