一、安装sqlite3 1、下载最新版的sqlite3,网址:http://www.sqlite.org/download.html
下载sqlite-autoconf-3130000.tar.gz 2、下载成功后解压并安装
tar -zxvf sqlite-autoconf-3130000.tar.gz
cd sqlite-autoconf-3130000
./configure
make
sudo make install
二、测试 在任意目录下新建一个数据库,比如sample 命令:sqlite3 sample 会出现如下提示: SQLite version 3.13.0 2016-05-18 10:57:30
Enter ".help" for usage hints.
sqlite> 输入 .help可以看到命令列表 输入sql语句create table user(username text primary key, password text); 建一张user表
输入sql语句insert into user values("tianyou121", "123"); 插入一个用户。
输入sql语句select * from user; 可以查看user表.
输入sql命令是记得结尾的'; '号。
用代码测试:
/*********************************************************************************
*Copyright:(C) 2016 huangan
*All rights reserved.
*
*Filename:sqlite.c
*Description:This file
*
*Version:1.0.0(05/24/2016)
*Author:huangan <894389300@qq.com>
*ChangeLog:1, Release initial version on "05/24/2016 07:18:01 AM"
*
********************************************************************************/#include
#include
#include int myfunc(void *p, int argc, char **argv, char **argvv)
{
int i;
*(int *)p = 0;
for(i =0;
i < argc;
i++)
{
printf("%s = %s ", argvv[i], argv[i] ? argv[i] :"NULL");
}putchar('\n');
return 0;
}int main(int argc, char *argv[])
{
sqlite3 *db;
char *err =0;
int ret =0;
int empty =1;
ret = sqlite3_open("sample",&db);
if(ret !=SQLITE_OK)
{
fputs("\n",stderr);
return -1;
}ret = sqlite3_exec(db, "select * from user;
", myfunc, &empty, &err);
if(ret != SQLITE_OK)
{
fputs(err,stderr);
fputs("\n",stderr);
sqlite3_close(db);
return -1;
}if(empty)
{
fputs("table student is empty\n", stderr);
return -1;
}sqlite3_close(db);
return -1;
}
将.c文件和数据库文件放在同一个目录下,并用GCC编译 :gcc sqlite.c -lsqlite3(PS:要加上-lsqlite3 指定库文件) ./a.out 就会输出:
文章图片
文章图片
OK,成功!!!!!