First 5 minutes of SQLite
What is SQLite?
SQLite is light-weight RDBMS, it is use the file system rather than the C/S client, it is written by C and was widely used in the embedded system, for instance, the iOS system.
How to use the database?
Since the database is written in C, it has some useful interface for C/C++, the user interface is. Basically we need to understand two useful objects, database_connection and prepared_statement they are just the pointer of the C struct. The database_connection object is sqlite3, and the prepared_statement object sqlite3_stmt.
You can control the 2 basic objects by varies routines provided by SQLite below:
sqlite3_open()
sqlite3_prepare()
sqlite3_step()
sqlite3_column()
sqlite3_finalize()
sqlite3_close()
How to run the SQL statement?
Create a prepared statement using sqlite3_prepare().
Evaluate the prepared statement by calling sqlite3_step() one or more times.
For queries, extract results by calling sqlite3_column() in between two calls to sqlite3_step().
Destroy the prepared statement using sqlite3_finalize().
Or you can use one wrapper routine instead, sqlite_exec, here’s the detail of this routine:
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */);
For instance, you can run statement like this:
char *errmsg;
const char *createSQL = "CREATE TABLE IF NOT EXISTS PEOPLE"
"(ID INTEGER PRIMARY KEY AUTOINCREMENT, FIELD_DATA TEXT)";
int result = sqlite3_exec(database, createSQL, NULL, NULL, &errmsg);
The result will be one integer of below:
#define SQLITE_OK 0 /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR 1 /* SQL error or missing database */
#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
#define SQLITE_PERM 3 /* Access permission denied */
#define SQLITE_ABORT 4 /* Callback routine requested an abort */
#define SQLITE_BUSY 5 /* The database file is locked */
#define SQLITE_LOCKED 6 /* A table in the database is locked */
#define SQLITE_NOMEM 7 /* A malloc() failed */
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL 13 /* Insertion failed because database is full */
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
#define SQLITE_EMPTY 16 /* Database is empty */
#define SQLITE_SCHEMA 17 /* The database schema changed */
#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
#define SQLITE_MISMATCH 20 /* Data type mismatch */
#define SQLITE_MISUSE 21 /* Library used incorrectly */
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
#define SQLITE_AUTH 23 /* Authorization denied */
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB 26 /* File opened that is not a database file */
#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
/* end-of-error-codes */
If your statement is run successfully, you will get the SQLITE_OK return code, otherwise, you will get more information of the error in the errmsg.
First 5 minutes of SQLite的更多相关文章
- [开源].NET高性能框架Chloe.ORM-完美支持SQLite
扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...
- 学习SQLite之路(四)
20160621 更新 参考: http://www.runoob.com/sqlite/sqlite-tutorial.html 1. SQLite alter命令:不通过执行一个完整的转储和数 ...
- Android SQLite (五 ) 全面详解(三)
SQLite约束 约束是在表的数据列上强制执行的规则.这些是用来限制可以插入到表中的数据类型.这确保了数据库中数据的准确性和可靠性.约束可以是列级或表级.列级约束仅适用于列,表级约束被应用到整个表. ...
- sqlite函数大全
abs(X) 返回参数X的绝对值. coalesce(X,Y,...) 返回第一个非空参数的副本.若所有的参数均为NULL,返回NULL.至少2个参数. glob(X,Y) 用于实现SQLite的 ...
- SQLite学习手册(内置函数)
一.聚合函数: SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持,因此我们这里将只是给出每个聚集函数的简要说明,而不在给出更多的示例了.这里还需要进一步说明的是,对于所有聚合函数而言, ...
- C# 封装 System.Data.SQLite
参考1: 关于如何使用System.Data.SQLite的入门: http://www.dreamincode.net/forums/topic/157830-using-sqlite-with-c ...
- sqlite 学习
到谷歌上搜sqlite,第一项便是官方网站:www.sqlite.org.进去后,先了解一下大体,感觉还不错. 进入Document页面,大标题SQLite Programming Interface ...
- (转)SQLite内置函数
一.聚合函数: SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持,因此我们这里将只是给出每个聚集函数的简要说明,而不在给出更多的示例了.这里还需要进一步说明的是,对于所有聚合函数而言, ...
- sqlite 日期型 字符串转为日期型
因为sqlite为弱引用,使用字段前将他强制转为日期型,用datetime.或者最原始的 strftime. SELECT distinct ID from testTable where datet ...
随机推荐
- 一、webpack那点事-安装、环境搭建
前言: 还记得两年前刚来公司才几个月,经理就安排我去做JS地图相关的维护和开发工作,然后就跟着一个公司老鸟(没俩月他离职了)熟悉地图相关的功能. 本人嘛,那会前端JS实际开发经验也才几个月,然后当我看 ...
- var foo= {} ;foo.method() 和 单例模式有什么区别
var Foo={} Foo.method = function() { var i = 0; return { a:function(){ console.log('aa', i++); }, b: ...
- 原生js和jquery实现图片轮播特效
本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下. 1)首先是页面的结构部分对于我这种左右切换 ...
- Sublime Text 2 注册码
----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E- 813A03DD 5E4AD9E6 6C0EEB94 BC99798F ...
- 阅读记录:Learning multiple layers of representation(杂乱笔记)
典型的浅层学习结构: 传统隐马尔可夫模型(HMM).条件随机场 (CRFs).最大熵模型(Maxent).支持向量机(SVM).核回归及仅含单隐层的多层感知器(MLP)等. 局部表示,分布式表示和稀疏 ...
- Ubuntu12.04 下修改Apache端口号
1:$sudo vim /etc/apache2/ports.conf NameVirtualHost *:80Listen 8090 #将此行的80修改成8090 2:sudo vim /etc/a ...
- SVN莫名出错,网上找遍无果,递归删除当前目录下所有.svn文件名
哎,太深刻的教训. 原来以前其它目录里有.SVN目录 ,而此SVN目录COPY到真正的SVN工作目录之后,会将有用的.SVN目录覆盖. 那么一样,显然,CI,UPDATE,CO之间的命令全部异常... ...
- keil Ax51中条件编译指令IF与$IF的区别
keil A51中条件编译指令IF与$IF的区别:1.IF和$IF是不等价的,不要混淆了;2.带前缀$的条件编译$IF用法:(汇编器指示命令Assembler Directive)只能用来测试由$SE ...
- Qt的Script、Quick、QML的关系与总结
背景 最近在学QML,感觉也不难,就是一直以来接触 Qt 的脚本类的东西的顺序是Script.Quick1.Declarative.Quick2.QML.那么每一个都是干什么的呢,这些东西搞的我有点混 ...
- poj1185:炮兵阵地(状压dp)
也算是比较基础的状压dp了,跟做过的第二道比较又稍微复杂了一点 需要记录之前两行的状态.. 统计结果也稍有不同 另外还学习了一个得到一个整数二进制位 1 的个数的位运算方法 详见代码: #includ ...