:c中使用sqlite3需要调用函数接口操作:
sqlite3 *db;
int status=sqlite_open("dbname",&db);//打开或者创建数据库
int status=sqlite3_exec(db,yuju,huitiaohanshu,,cuowuzhizhen);//数据库所有的操作都得通过这个函数执行
sqlite3_close(db);//使用完后要关闭数据库资源
:sqlite3语句:
建表:
create table pic([picId] integer PRIMARY KEY AUTOINCREMENT, [InsertTime] TimeStamp NOT NULL DEFAULT (datetime('now','localtime')), [url] varchar());
//创建了一个有三个字段(picId,url,inserttime)并且这里的插入时间为自动插入当前当地时间
约束条件:
not null:
unique:唯一
primary key:主键
foreign key:外键(创建该表和父表之间的联系)
check:对该项输入内容的条件检查
default:默认值
数据类型:(相似匹配,会自动寻找比较合适的具体数据类型进行匹配)
integer:
int integer int2 int8 (unsigned big int) (big int)
text:
character() varchar() text clob....
none:
real:
real double float..
numeric:
boolean data datetime
create table teacher(id integer primary key auto increment);
create table stu (id integer primary key autoincrement,
name varchar() check(length(name)>),
tel varchar() not null default '',
cls integer not null ,
unique(name,tel),//设置name和tel的组合唯一
foreign key(cls) references teacher(id));//绑定两个表中的id
插入:
insert into pic([picId],[url]) values(null,'%s');
//当每个字段都要插入时可以缺省表后面的参数
insert into stu1 select * from stu;
//将一个表的所有内容导入另外一个
查询:
select * from pic;
select name from stu where id=;
select id from stu order by id;//由id排序输出
select * from stu where name like "t%";//找到stu中名字以t开头的数据
select * from stu group by id having id>;//查询id>2的数据并且按照id分组
select * from stu limit offset ;//从索引2开始输出后面一个数据
//c语言中查询一般是使用的回调,在执行sql语句的时候就传入查询数据以后应该怎么处理的函数

例子:

#include <stdio.h>
#include<time.h>
#include <sqlite3.h>
#include<string.h> //查询的回调函数声明
int select_callback(void * data, int col_count, char ** col_values, char ** col_Name); int main(int argc, char * argv[])
{
const char * sSQL1 = "create table pic([picId] integer PRIMARY KEY AUTOINCREMENT, [InsertTime] TimeStamp NOT NULL DEFAULT (datetime('now','localtime')), [url] varchar(20));";
char * pErrMsg = ;
int result = ;
// 连接数据库
sqlite3 * db = ;
int ret = sqlite3_open("./test9.db", &db);
if( ret != SQLITE_OK ){
fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));
return();
}
printf("数据库连接成功!\n"); // 执行建表SQL
sqlite3_exec( db, sSQL1, , , &pErrMsg );
if( ret!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", pErrMsg);
sqlite3_free(pErrMsg);
return ;
}
printf("建表成功!\n"); // 执行插入记录SQL
//result = sqlite3_exec( db, "insert into pic([url]) values('/c');", 0, 0, &pErrMsg);
int i;
for(i=;i<;i++){
if(sqlite3_exec( db, "insert into pic([picId],[url]) values(null,'/c')", , , &pErrMsg)!= SQLITE_OK){
fprintf(stderr, "insert SQL error: %s\n", pErrMsg);
sqlite3_free(pErrMsg);
printf("插入失败!\n");
}else{
printf("插入数据成功\n");
}
}
// 查询数据表
printf("开始查询数据库内容\n");
//sqlite3_exec( db, "select * from pic;", select_callback, 0, &pErrMsg);
if(sqlite3_exec( db, "select * from pic;", select_callback, , &pErrMsg)!=SQLITE_OK){
fprintf(stderr, "insert SQL error: %s\n", pErrMsg);
}else{
printf("查询失败 \n");
}
// 关闭数据库
sqlite3_close(db);
db = ;
printf("数据库关闭成功!\n");
return ;
} int select_callback(void * data, int col_count, char ** col_values, char ** col_Name)
{
// 每条记录回调一次该函数,有多少条就回调多少次
int i;
for( i=; i < col_count; i++){
printf( "%s = %s\n", col_Name[i], col_values[i] == ? "NULL" : col_values[i] );
} return ;
}

linux 使用sqlite3的更多相关文章

  1. 基于s5pv210嵌入式linux系统sqlite3数据库移植

    基于s5pv210嵌入式linux系统sqlite3数据库移植 1.下载源码 http://www.sqlite.org/download.html 最新源码为3080100 2.解压 tar xvf ...

  2. linux下sqlite3可视化工具

    1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库. 2.安装:要安装sqlite3,可以在终端提示符后运行下列命令:sudo apt-get install sqlite3 ...

  3. Linux下sqlite3编程

    ---------------------------------------------------------------------------------------------------- ...

  4. Linux 中 sqlite3 基本操作

    https://www.runoob.com/sqlite/sqlite-commands.html 一 .linux 下安装数据库和创建一个数据库 1. Linux 下安装sqlite3 需要两个命 ...

  5. linux 安装sqlite3

    python2个版本导致的问题. 网上找了好多方法都不行. 最后自己莫名其妙弄好了, 回想了一下大概是 安装sqlite3 重新安装python 最后 yum update 更新 就好了.

  6. linux装sqlite3

    下载sqlite3源码包 tar xvfz sqlite-src-3.3.5 cd sqlite-3.3.5 ./configure –no-tcl make python继续一次. apt inst ...

  7. linux sqlite3 相关

    数据库sqlite 1 用下载好的安装包安装 linux@ubuntu:~/sqlite3$ sudo dpkg -i libsqlite3-0_3.7.2-1ubuntu0.1_i386_1.deb ...

  8. 如何在Linux下用C/C++语言操作数据库sqlite3(很不错!设计编译链接等很多问题!)

    from : http://blog.chinaunix.NET/uid-21556133-id-118208.html 安装Sqlite3: 从www.sqlite.org上下载Sqlite3.2. ...

  9. Linux下用到数据库sqlite3

    最近在Linux下用到数据库sqlite3,于是开始了该方面的学习. 0. 引言 我们这篇文章主要讲述了如何在C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理, 包括创建数据库.创建 ...

随机推荐

  1. SpringBoot | 第三十五章:Mybatis的集成和使用

    前言 最近收到公众号留言说,单纯的Mybatis的集成和使用.前面在第九章:Mybatis-plus的集成和使用介绍了基于mybatis-plus的集成和使用.后者也只是对mybatis进行了功能增强 ...

  2. 记自己的hexo个人博客

    https://othercoding.github.io/

  3. 对C++ Local的经典分析(转)

    对C++ Local的经典分析 本贴转载自:再别流年的技术实验室 文章地址: http://kittsoft.xp3.biz/?p=86 “这个问题比你想象中复杂”(我也学下BS的风格,虽然这句话是我 ...

  4. bzoj 3243: [Noi2013]向量内积

    Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...

  5. Git使用教程,感觉比较全,所以【转载】

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  6. XML深入了解(XML JavaSprint)

    XMLHttpRequest 对象 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是开发者的梦想,因为您能够: 在不重新加载页面的情况下更新网页 在 ...

  7. JAVA常见中文问题的解决方法(转)

    JAVA常见中文问题的解决方法 http://www.java-cn.com/club/article-5876-1.html 以下解决方案是笔者在日常生活中遇到的,希望能对你解决JAVA中文问题有所 ...

  8. sublime常用设置

    原文地址 https://segmentfault.com/a/1190000002596724 前言 Sublime Text3 在文中简称为ST. ST是个不错的编辑器,我用了有段时间了,所以我觉 ...

  9. html-其他常见标签的使用

    b:加粗 s:删除线 u:下划线 i:斜体 per:原样输出 sup:上标 sub:下标 p:段落标签 比br多一行 (CSS) div:自动换行 span:在一行显示 完整代码: <html& ...

  10. IntelliJ IDEA热部署教程,只要两步!

    一.开启idea自动build功能1.File -> Settings -> Build,Execution,Deployment -> Compiler -> Build p ...