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示例程序下 ...
随机推荐
- sqlserver查询指定树形结构的所有子节点
用标准sql的with实现递归查询(sql2005以上肯定支持,sql2000不清楚是否支持): with subqry(id,name,pid) as ( select id,name,pid fr ...
- C#获取cpu序列号 硬盘ID 网卡硬地址以及操作注册表 .
转:http://blog.csdn.net/smartsmile2012/article/details/8682295 #region 获取cpu序列号 硬盘ID 网卡硬地址 /**/ /// & ...
- strlen的C/C+++实现
2013-07-05 11:36:05 小结: 本函数给出了几种strlen的实现,有ugly implementation,也有good implementation.并参考标准库中的impleme ...
- android 电容屏(一):电容屏基本原理篇
平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...
- UVa 1645 (递推) Count
题意: 有多少个n个节点的有根树,满足每层节点的子节点个数相同,输出该数目除以1e9+7的余数. 分析: 这种题目就属于那种,看起来很高冷,读完题更高冷.想了N久想不出来,一搜题解,卧槽,这么sb的题 ...
- BZOJ2882: 工艺
题解: 裸的字符串最小表示... 可以戳这里:http://www.cnblogs.com/ACAC/archive/2010/05/23/1742349.html 这里说一下为什么a[i+k]> ...
- [Mac][phpMyAdmin] [2002] No such file or directory
我从phpMyAdmin的官网下载了最新版,将它解压到 /Library/WebServer/Documents 下,然后把文件夹改名 phpmyadmin . 接着输入在浏览器中输入 localho ...
- 如何使用UDP进行跨网段广播
广播域首先我们来了解一下广播域的概念.广播域是网络中能接收任一台主机发出的广播帧的所有主机集合.也就是说,如果广播域内的其中一台主机发出一个广播帧,同一广播域内所有的其它主机都可以收到该广播帧.广播域 ...
- [转] windows7 IIS管理器 在计算机“.”上没有找到WAS服务
原文地址:windows7 IIS管理器 在计算机"."上没有找到WAS服务作者:云中的风 OS:windows7旗舰版 产生问题原因:运行金蝶K3-HR时客户端提示中间层服务器不 ...
- [转] Envelop
xiaohaidepoli原文Envelop Envelope也称包络线,是一个矩形区域,是每个几何形体的最小外接矩形.每个Geometry都拥有一个Envelope,包括Envelope自身. 它定 ...