Sqlite数据库简介
在应用sqlite之前需要添加sqlite库,那么我们就会发现有3和3.0的区别,开始我也并不懂,后才知道:
实际上libsqlite3.dylib本身是个链接,它指向libsqlite3.0.dylib。
也就是说在项目里如果你添加libsqlite3.dylib和添加libsqlite3.0.dylib其实是添加了同一个文件,从而使得该库常新。
为了方便管理数据库,并且使之唯一不出现混乱,我们尝尝将sqlite定义一个单例
static MySqliteManager *manager = nil;
+(MySqliteManager *)shareManager{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[MySqliteManager alloc]init];
});
return manager;
}
singleton
sqlite数据库可以分为以下几部分:
1、打开数据库
(1)如果没有该文件则创建空的sqlite后缀文件。
(2)允许对数据进行操作
- (void)open{
//确定数据库的存放路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// sqlite路径
NSString *sqlitePath = [documentPath stringByAppendingString:@"dataBase.sqlite"];
NSLog(@"path == %@",sqlitePath);
//打开数据库
int result = sqlite3_open(sqlitePath.UTF8String, &db);//参数1:C中字符串,(使用点方法UTF8String转换 , )
// 判断打开是否成功
if (result == SQLITE_OK) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}
}
open
2、关闭数据库
(1)禁止对数据进行任何操作
- (void)close{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}
}
close
3、创建表
(1)创建对应的数据结构
(2)其中 primary key 指唯一,不可重复
- (void)create{
//sql语句
NSString *sqlString = @"create table Person ( id integer primary key, name text ,age integer)";
// 执行sql语句
char *error;
sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error);
if (error == nil) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}
}
creat
4、插入数据
(1)添加具体的数据实例
//sql语句
NSString *sqlString = @"insert into Person ('name','age') values ('张三',18)";
// 执行语句
char *error = nil;
sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error);
if (error == nil) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}
insert
5、更新数据
(1)修改数据
//sql语句
NSString *sqlString = @"update Person set 'name' = '李四' where id = 1";
//执行sql语句 char *error = nil;
sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error);
if (error == nil) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"更新成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"更新失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}
update
6、删除数据
//sql语句
NSString *sqlString = @"delete from Person where id = 1";
//执行sql语句
char *error = nil;
sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error);
if (error == nil) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
[alertView show];
}
delete
7、查询数据
//sql语句
NSString *sqlString = @"select *from Person";
//准备sql语句 sqlite3_stmt *stmt = nil;//数据库管理指针
sqlite3_prepare(db, sqlString.UTF8String, -/*-1代表自动计算语句长度*/, &stmt, nil); // 单步执行语句
while (sqlite3_step(stmt) == SQLITE_ROW) {
int ID = sqlite3_column_int(stmt, );//第二个参数是参数位置
const unsigned char * name = sqlite3_column_text(stmt, );
NSString *nameString = [NSString stringWithUTF8String:(const char *)name];
int age= sqlite3_column_int(stmt, ); NSLog(@"id = %d \n name = %@ \n age = %d",ID,nameString,age);
}
// 释放管理语句
sqlite3_finalize(stmt);
select
常用语句:
1 首先获取iPhone上sqlite3的数据库文件的地址
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"database_name"];
2 打开iPhone上的sqlite3的数据库文件
sqlite3_open([path UTF8String], &database);
3 准备sql文---sql语句
const char *sql = "SELECT * FROM table_name WHERE pk=? and name=?";
sqlite3_prepare_v2(database, sql, -1, &stmt, NULL);
4 邦定参数
sqlite3_bind_int(stmt, 1, 1);
// 邦定第二个字符串参数
sqlite3_bind_text(stmt, 2, [title UTF8String], -1, SQLITE_TRANSIENT);
5 执行sql文
6 释放sql文资源
7 关闭iPhone上的sqlite3的数据库
http://hi.baidu.com/clickto/blog/item/0c6904f787c34125720eec87.html
以下演示一下使用sqlite的步骤,先创建一个数据库,然后查询其中的内容。2个重要结构体和5个主要函数:
sqlite3 *pdb, 数据库句柄,跟文件句柄FILE很类似
sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句
sqlite3_open(), 打开数据库
sqlite3_exec(), 执行非查询的sql语句
sqlite3_prepare(), 准备sql语句,执行select语句或者要使用parameter bind时,用这个函数(封装了sqlite3_exec).
Sqlite3_step(), 在调用sqlite3_prepare后,使用这个函数在记录集中移动。
Sqlite3_close(), 关闭数据库文件
还有一系列的函数,用于从记录集字段中获取数据,如
sqlite3_column_text(), 取text类型的数据。
sqlite3_column_blob(),取blob类型的数据
sqlite3_column_int(), 取int类型的数据
PreparedStatement方式处理SQL请求的过程
特点:可以绑定参数,生成过程。执行的时候像是ADO一样,每次返回一行结果。
1. 首先建立statement对象:
int sqlite3_prepare(
sqlite3 *db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nBytes, /* Length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);
2. 绑定过程中的参数(如果有没有确定的参数)
int sqlite3_bind_xxxx(sqlite3_stmt*, int, ...);
第二个int类型参数-表示参数的在SQL中的序号(从1开始)。
第三个参数为要绑定参数的值。
对于blob和text数值的额外参数:
第四参数是字符串(Unicode 8or16)的长度,不包括结束'\0'。
第五个参数,类型为void(*)(void*),表示SQLite处理结束后用于清理参数字符串的函数。
没有进行绑定的未知参数将被认为是NULL。
3. 执行过程
int sqlite3_step(sqlite3_stmt*);
可能的返回值:
*SQLITE_BUSY: 数据库被锁定,需要等待再次尝试直到成功。
*SQLITE_DONE: 成功执行过程(需要再次执行一遍以恢复数据库状态)
*SQLITE_ROW: 返回一行结果(使用sqlite3_column_xxx(sqlite3_stmt*,, int iCol)得到每一列的结果。
再次调用将返回下一行的结果。
*SQLITE_ERROR: 运行错误,过程无法再次调用(错误内容参考sqlite3_errmsg函数返回值)
*SQLITE_MISUSE: 错误的使用了本函数(一般是过程没有正确的初始化)
4. 结束的时候清理statement对象
int sqlite3_finalize(sqlite3_stmt *pStmt);
应该在关闭数据库之前清理过程中占用的资源。
5. 重置过程的执行
int sqlite3_reset(sqlite3_stmt *pStmt);
过程将回到没有执行之前的状态,绑定的参数不会变化。
Sqlite数据库简介的更多相关文章
- SQLite数据库 简介、特点、优势、局限性及使用
SQLite简介 SQLite是一个进程内的轻量级嵌入式数据库,它的数据库就是一个文件,实现了自给自足.无服务器.零配置的.事务性的SQL数据库引擎.它是一个零配置的数据库,这就体现出来SQLite与 ...
- System.Data.SQLite数据库简介
SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义.表.索引和数据本身 ...
- C#中使用SQLite数据库简介(上)
[SQLite数据库] SQLite是一个开源的轻量级的桌面型数据库,它将几乎所有数据库要素(包括定义.表.索引和数据本身)都保存在一个单一的文件中.SQLite用C编写实现,它在内存消耗.文件体积. ...
- SQLite数据库简介(转)
大家好,今天来介绍一下SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准 ...
- C#中使用SQLite数据库简介(下)
[SQLite管理工具简介] 推荐以下2款: Navicat for SQLite:功能非常强大,几乎包含了数据库管理工具的所有必需功能,操作简单,容易上手.唯一的缺点是不能打开由System.Dat ...
- SQLite数据库简介和使用
一.Sqlite简介: SQLite (http://www.sqlite.org/),是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中 ...
- 数据库 简介 升级 SQLite 总结 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- SQLite数据库增删改查
一:SQLite数据库简介: SQLite是一种轻量级的关系型数据库,官网:http://www.sqlite.org/. SQLite数据库文件存在于移动设备的一下目录中:data->data ...
- 【Android 应用开发】Android 数据存储 之 SQLite数据库详解
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...
随机推荐
- HTTP协议 Servlet入门 Servlet工作原理和生命周期 Servlet细节 ServletConfig对象
1 HTTP协议特点 1)客户端->服务端(请求request)有三部份 a)请求行--请求行用于描述客户端的请求方式.请求的资源名称,以及使用的HTTP协议版本号 请求行中的GET ...
- Eclipse中将classes文件删除之后显示:找不到或无法加载主类解决方案
第一步: 将Eclipse自动编译打开 Project -> Build Automatically 第二步: Eclipse - Project - Clean
- UVa 1395 (最小生成树) Slim Span
题意: 规定一棵生成树的苗条度为:最大权值与最小权值之差.给出一个n个顶点m条边的图,求苗条度最小的生成树. 分析: 按照边的权值排序,枚举边集的连续区间[L, R]的左边界L,如果这些区间刚好满足一 ...
- 我个人有关 Azure 网络 SLA、带宽、延迟、性能、SLB、DNS、DMZ、VNET、IPv6 等的 Azure 常见问题解答
Igor Pagliai(微软) 2014 年 9月 28日上午 5:57 年 11 月 3 年欧洲 TechEd 大会新宣布的内容). 重要提示:这篇文章中我提供的信息具有时间敏感性,因为这些 ...
- 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符
// 从第一个字符串中删除第二个字符串中出现过的所有字符 #include <stdio.h> char* remove_second_from_first( char *first, c ...
- 新手!SDK Manager里找不到API安装的选项怎么办?
只有Tools和EXTRAS文件夹的选项,没有API包安装,咋办呢? 回复讨论(解决方案) 网络有问题吗? 网络有问题吗? 就是不知道啊 你是在eclispe里面打开的?还是在外面直接打开的?没有 ...
- WCF 学习总结2 -- 配置WCF
前面一篇文章<WCF 学习总结1 -- 简单实例>一股脑儿展示了几种WCF部署方式,其中配置文件(App.config/Web.config)都是IDE自动生成,省去了我们不少功夫.现在回 ...
- Apache-AB压力测试实例
一 AB背景介绍 Apache附带的压力测试工具apache bench--简称ab,非常容易使用,并且完全可以摸你各种条件对Web服务器发起测试请求.ab可以直接在Web服务器本地发起测试请求,这对 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- java开发 时间类型的转换
1.String转date SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Strin ...