linux 使用sqlite3
: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的更多相关文章
- 基于s5pv210嵌入式linux系统sqlite3数据库移植
基于s5pv210嵌入式linux系统sqlite3数据库移植 1.下载源码 http://www.sqlite.org/download.html 最新源码为3080100 2.解压 tar xvf ...
- linux下sqlite3可视化工具
1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库. 2.安装:要安装sqlite3,可以在终端提示符后运行下列命令:sudo apt-get install sqlite3 ...
- Linux下sqlite3编程
---------------------------------------------------------------------------------------------------- ...
- Linux 中 sqlite3 基本操作
https://www.runoob.com/sqlite/sqlite-commands.html 一 .linux 下安装数据库和创建一个数据库 1. Linux 下安装sqlite3 需要两个命 ...
- linux 安装sqlite3
python2个版本导致的问题. 网上找了好多方法都不行. 最后自己莫名其妙弄好了, 回想了一下大概是 安装sqlite3 重新安装python 最后 yum update 更新 就好了.
- linux装sqlite3
下载sqlite3源码包 tar xvfz sqlite-src-3.3.5 cd sqlite-3.3.5 ./configure –no-tcl make python继续一次. apt inst ...
- linux sqlite3 相关
数据库sqlite 1 用下载好的安装包安装 linux@ubuntu:~/sqlite3$ sudo dpkg -i libsqlite3-0_3.7.2-1ubuntu0.1_i386_1.deb ...
- 如何在Linux下用C/C++语言操作数据库sqlite3(很不错!设计编译链接等很多问题!)
from : http://blog.chinaunix.NET/uid-21556133-id-118208.html 安装Sqlite3: 从www.sqlite.org上下载Sqlite3.2. ...
- Linux下用到数据库sqlite3
最近在Linux下用到数据库sqlite3,于是开始了该方面的学习. 0. 引言 我们这篇文章主要讲述了如何在C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理, 包括创建数据库.创建 ...
随机推荐
- nyoj 47——过河问题——————【贪心】
过河问题 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不敢过桥去的 ...
- docker-compose 安装solr+ikanalyzer
docker-compose.yml version: '3.1' services: solr: image: solr restart: always container_name: solr p ...
- ExcelHelper office 导出
要是服务器上没有装着excel 可以用c#导出excel表吗 2009-08-10 17:36 风之成 | 分类:办公软件 | 浏览2279次 租用的空间 服务器上没有装着office excel,可 ...
- 项目搭建系列之三:SpringMVC框架下使用Ehcache对象、数据缓存
注明:该文章为以前写的文章,这里只更改了标题,无GitHub源码下载. 一.准备工作 如果已经成功搭建SpringMVC环境,那么就可以进入Ehcache的准备工作了.1.下载jar包 Ehca ...
- 研究SSIS时候的一些常见错误
1.[OLE DB 目标 [59]] 错误: SSIS 错误代码 DTS_E_OLEDBERROR.出现 OLE DB 错误.错误代码: 0x80004005.已获得 OLE DB 记录.源:“Mic ...
- VMware装Linux系统全屏问题
在VMware上出装Linux,有强迫症的患者总是无法接受它不能全屏的问题,当然网上也有该问题的解决方案,但是搜索出来的答案总是零零散散,让很多初学者望而却步!今天笔者根据自己的机遇总结一遍最完备的解 ...
- 初识Socket通信:基于TCP和UDP协议学习网络编程
学习笔记: 1.基于TCP协议的Socket网络编程: (1)Socket类构造方法:在客户端和服务器端建立连接 Socket s = new Socket(hostName,port);以主机名和端 ...
- Fragment中的方法findFragmentById(int id)的返回值探讨
在学习<Android编程权威指南>P124页的时候,遇到了这样的代码: 引起了我的疑问if的判断条件是(fragment==null),那执行完上一句 Fragment Fragment ...
- Spring课程 Spring入门篇 4-7 Spring bean装配之基于java的容器注解说明--@Scope 控制bean的单例和多例
1 解析 1.1 bean的单例和多例的应用场景 1.2 单例多例的验证方式 1.3 @Scope注解单例多例应用 2 代码演练 2.1 @Scope代码应用 1 解析 1.1 bean的单例和多例的 ...
- 科学计算基础包——Numpy
一.NumPy简介 NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. 1.NumPy的主要功能 (1)ndarray:一个多维数组结构,高效且节省空间. (2)无需 ...