在iOS中实现SQLite数据库的操作:1.导入框架(libsqlite3.0.tbd) 2.导入头文件<sqlite3.h> 3.实现数据的增删改查

实现简单 SQLite数据库操作 的 demo 具体过程:

1.创建名为 SQLite_Manage 的.h .m 文件,导入头文件 <sqlite3.h>

2.数据库在一个app中只有一个,使用单例模式:(代码如下)

 + (SQLite_Manager *)sharedManager{
static SQLite_Manager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[SQLite_Manager alloc]init];
});
return manager;
}

3.打开数据库,代码如下:

 - (void)open{
//document路径
NSString *docment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//sqlite 路径
NSString *sqlitePath = [docment stringByAppendingPathComponent:@"database.sqlite"];
//打开数据库
int result = sqlite3_open(sqlitePath.UTF8String, &db);
//判断数据库是否打开成功
if (result == SQLITE_OK) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}

4.创建表,代码如下:

 - (void)creatTable{
//sql语句
NSString *sqlString = @"create table Person (id integer primary key,name text,age integer)";
//执行SQL语句
char *error = nil;
sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); //判断是否出现了错误
if (error == nil){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}

5.插入数据,代码如下:

 - (void)insert{
//sql语句
NSString *sqlString = @"insert into Person ('name','age') values ('Ager',18)";
//执行SQL语句
char *error = nil;
sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error);
//判断是否出现了错误
if (error == nil){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入数据成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入数据失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
} }

6.修改数据,代码如下:

 - (void)update{
//sql语句
NSString *sqlString = @"update Person set 'name' = 'Arun' 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:nil otherButtonTitles:@"确定", nil];
[alertView show];
}else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"数据更新失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}

7.查询数据,代码如下:

 - (void)select{
//sql语句
NSString *sqlString = @"select * from Person";
//准备sql
sqlite3_stmt *stmt = nil;
sqlite3_prepare(db, sqlString.UTF8String,-, &stmt, nil);
//单步执行语句
while (sqlite3_step(stmt) == SQLITE_ROW) {
int ID = sqlite3_column_int(stmt, );
const unsigned char *name = sqlite3_column_text(stmt, );
int age = sqlite3_column_int(stmt, );
NSLog(@"%d,%s,%d",ID,name,age);
}
sqlite3_finalize(stmt);
}

8.删除数据,代码如下:

 - (void)deleteData{
//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:nil otherButtonTitles:@"确定", nil];
[alertView show];
}else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}

9.关闭数据库,代码如下:

 - (void)close{
//关闭数据库
int result = sqlite3_close(db);
//判断数据库是否关闭成功
if (result == SQLITE_OK) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}

iOS 中SQLite数据库操作的更多相关文章

  1. Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库

    下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...

  2. QF——iOS中的数据库操作:SQLite数据库,第三方封装库FMDB,CoreData

    SQLite数据库: SQLite是轻量级的数据库,适合应用在移动设备和小型设备上,它的优点是轻量,可移植性强.但它的缺点是它的API是用C写的,不是面向对象的.整体来说,操作起来比较麻烦.所以,一般 ...

  3. 我的Android六章:Android中SQLite数据库操作

    今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...

  4. Android中SQLite数据库操作(2)——SQLiteOpenHelper类

    如果开发者对SQL语法不熟悉,我要告诉你一个好消息,Android提供了一个SQLiteOpenHelper类. 在实际项目中很少使用SQLiteDatabase的方法(请看:http://blog. ...

  5. Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操作数据库

    如果开发者对SQL语法不熟,甚至以前从未使用过任何数据库,Android的SQLiteDatabase提供了insert.update.delete或query语句来操作数据库. 一.insert方法 ...

  6. 在安卓开发中使用SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  7. android中的数据库操作(SQLite)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

  8. Flutter中SQLite数据库的使用

    同时支持android和ios 支持事务和批量操作 支持插入/查询/更新/删除操作 在iOS和Android上的后台线程中执行数据库操作 1.添加依赖 dependencies: ... sqflit ...

  9. iOS中的数据库应用

    iOS中的数据库应用 SLQLite简介 什么是SQLite SQLite是一款轻型的嵌入式数据库 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 它的处理速度比Mysql.Post ...

随机推荐

  1. 【思考】由安装zabbix至排障php一系列引发的思考

    [思考]由安装zabbix至排障php一系列引发的思考 linux的知识点林立众多,很有可能你在排查一个故障的时候就得用到另一门技术的知识: 由于linux本身的应用依赖的库和其它环境环环相扣,但又没 ...

  2. [转]ASP.NET MVC Select List Example

    本文转自:http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example Select lists are a great wa ...

  3. 关于Spring中AOP的理解

    AOP简介[理解][重点] 1.AOP(Aspect Oriented Programing)面向切面/方面编程 2.AOP隶属软件工程的范畴,指导开发人员如何制作开发软件,进行结构设计 3.AOP联 ...

  4. 哇!今天找到一个非常好用的自动补全插件-necomplete.vim

    看别人说的什么xpcomplete,snipte,拿来都不会用,这个necomplete.vim还挺好用的,不用去按C-X,C-O进行补全,把关键字自动的列出来,调用的是用户自定义补全,^u^n^p的 ...

  5. 使用PSSH批量SSH操作Linux服务器

    http://www.opstool.com/article/266 服务器多了,有一个烦恼就是如何批量快速操作一堆服务器.这里我推荐一下经常使用利器pssh.这个工具给我的工作带来了莫大的帮助. 简 ...

  6. 利用CodeSmith生成抽象工厂步骤

    其实CodeSmith挺好的,帮我们主动生成不少代码,并且代码质量不错,下面就来介绍一下利用CodeSmith生成抽象工厂步骤 打开codesmith模板的buildall 注意path的设置,因为后 ...

  7. DOM4J方式解析XML文件

    dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory dom4j是一个简单的开源库,用于处理XML. X ...

  8. javascript学习笔记20160121-css选择器

    元素可以用id.标签名或类来描述: 更一般的,元素可以基于属性来选取: 这些基本的选择器可以组合使用: 选择器可以指定文档结构(重要,之前一直不太明白>的使用): 选择器可以组合起来选取多个或多 ...

  9. Linux启动提示“unexpected inconsistency;RUN fsck MANUALLY”

    问题:在开机启动时,提示“unexpected inconsistency;RUN fsck MANUALLY”进不了系统 解决方法: fsck不仅可以对文件系统进行扫描,还能修正文件系统的一些问题, ...

  10. OpenJudge/Poj 2027 No Brainer

    1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...