iOS 中SQLite数据库操作

时间:2023-02-26 11:17:16

在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六章&colon;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. C&num; 程序集反射

    namespace AssemblyLibrary { public class AssemblyLibrary { public static object LoadAssembly(string ...

  2. 图论&lpar;对偶图&rpar;:COGS 470&period; &lbrack;NOI2010&rsqb;海拔

    470. [NOI2010]海拔 ★★★☆   输入文件:altitude.in   输出文件:altitude.out   简单对比 时间限制:2 s   内存限制:512 MB 海拔 [问题描述] ...

  3. config large memory

    C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...

  4. 如何截取url中的各个参数?

    在页面跳的时候,目的界面可能会根据url中的某些参数进行数据处理,这个时候如何能快速并设计一个通用的截取url中的参数,并且获取各个参数值? 代码: url = location.search;//获 ...

  5. ES6数组及数组方法

    ES6数组可以支持下面的几种写法: (1)var [a,b,c] = [1,2,3]; (2)var [a,[[b],c]] = [1,[[2],3]]; (3)let [x,,y] = [1,2,3 ...

  6. PHP的数据类型和魔术常量

    一. 1.boolean 布尔类型 (布尔值本身,整型0,浮点型0.0,空字符串,不包含任何元素的数组,不包括任何成员变量的对象 NULL,未赋值的变量) 2.integer 整型 3.float 浮 ...

  7. Git两分钟指南-学习入门参考

    Git两分钟指南 http://blog.jobbole.com/78999/ GIT和SVN之间的五个基本区别 http://www.oschina.net/news/12542/git-and-s ...

  8. ubuntu设置目录容量大小

    1:方法如下 sudo dd if=/dev/zero of=/root/disk1.img bs=2M count=10      //          2M*10=20M    zero 是de ...

  9. CF808D STL

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  10. lodash&lpar;一&rpar;数组

    前言: lodash是一个具有一致接口.模块化.高性能等特性的JavaScript工具库(官网地址:http://lodashjs.com/docs/#_differencearray-values) ...