: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. 绘图神器-matplotlib入门

    这次,让我们使用一个非常有名且十分有趣的玩意儿来完成今天的任务,它就是jupyter. 一.安装jupyter matplotlib入门之前,先安装好jupyter.这里只提供最为方便快捷的安装方式: ...

  2. File 类 的基本操作

    //  File 类(静态类)   File 的缺点:只能用来读小文件 (它是一下子全都读进去) //创建一个文件 // File.Create(@"C:\Users\wbrm\Deskto ...

  3. css-知识总结

    是什么 CSS通常称为CSS样式或层叠样式表,主要用于设置HTML页面中的文本内容(字体,大小,对其方式等),图片的外形 (高宽.边框样式.边距等)以及版面的布局等外观显示样式. CSS可以是HTML ...

  4. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  5. hibernate的查询 (比较get 与load)

    hibernate的查询的比较hibernate的查询有很多,Query,find,Criteria,get,load query使用hsql语句,可以设置参数是常用的一种方式 criteria的方式 ...

  6. LOJ#2552. 「CTSC2018」假面(期望 背包)

    题意 题目链接 Sol 多年以后,我终于把这题的暴力打出来了qwq 好感动啊.. 刚开始的时候想的是: 设\(f[i][j]\)表示第\(i\)轮, 第\(j\)个人血量的期望值 转移的时候若要淦这个 ...

  7. 巡风扫描器安装-windows部署

    巡风是一款适用于企业内网的漏洞快速应急,巡航扫描系统. 作者github地址 https://github.com/ysrc/xunfeng 一.环境安装 1,安装Python解释器 https:// ...

  8. Mybatis 的多个数据源

    主要思路: DataSource --> SqlSessionFactory --> SqlSessionTemplate 第一方式: 定义多套  DataSource --> Sq ...

  9. 生成对抗式网络 GAN的理解

    转自:https://zhuanlan.zhihu.com/p/24767059,感谢分享 生成式对抗网络(GAN)是近年来大热的深度学习模型.最近正好有空看了这方面的一些论文,跑了一个GAN的代码, ...

  10. matlab练习程序(Ritter‘s最小包围圆)

    原始算法是sphere,我这里简化为circle了. Ritter's求最小包围圆为线性算法,因为非常简单,所以应用非常广泛. 该算法求出的圆比最优圆大概会大个5%到20%左右,求最优圆应该可以用Bo ...