iOS 中SQLite数据库操作
在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数据库操作的更多相关文章
- Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库
下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...
- QF——iOS中的数据库操作:SQLite数据库,第三方封装库FMDB,CoreData
SQLite数据库: SQLite是轻量级的数据库,适合应用在移动设备和小型设备上,它的优点是轻量,可移植性强.但它的缺点是它的API是用C写的,不是面向对象的.整体来说,操作起来比较麻烦.所以,一般 ...
- 我的Android六章:Android中SQLite数据库操作
今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...
- Android中SQLite数据库操作(2)——SQLiteOpenHelper类
如果开发者对SQL语法不熟悉,我要告诉你一个好消息,Android提供了一个SQLiteOpenHelper类. 在实际项目中很少使用SQLiteDatabase的方法(请看:http://blog. ...
- Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操作数据库
如果开发者对SQL语法不熟,甚至以前从未使用过任何数据库,Android的SQLiteDatabase提供了insert.update.delete或query语句来操作数据库. 一.insert方法 ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- android中的数据库操作(SQLite)
android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库 an ...
- Flutter中SQLite数据库的使用
同时支持android和ios 支持事务和批量操作 支持插入/查询/更新/删除操作 在iOS和Android上的后台线程中执行数据库操作 1.添加依赖 dependencies: ... sqflit ...
- iOS中的数据库应用
iOS中的数据库应用 SLQLite简介 什么是SQLite SQLite是一款轻型的嵌入式数据库 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 它的处理速度比Mysql.Post ...
随机推荐
- delta
1,安装synplyfy:综合工程,便于学习(模块间的关系,数据流向) 2,安装wps office: www.wps.com/linux,论坛有安装方法和依赖包处理 3,安装kmplayer: 4 ...
- angularjs Failed to read the 'selectionStart' property from 'HTMLInputElement':
在找angularjs input(type='number')在获取焦点的时候,文本框内容选中效果,参考了:Select text on input focus,我直接复制他的code之后,在ion ...
- C#学习笔记1:正则表达式和数据库连接
1.using System.Text.RegularExpressions; 只有导入该命名空间,才能引入Regex对象,IsMatch是Regex中的一个方法,作用是来判断(输入)内容是否满足正则 ...
- Android——列表选择框(Spinner)
通常情况下,如果列表选择框中要显示的列表项是可知的,那么可以将其保存在数组资源文件中,然后通过数组资源来为列表选择框指定列表项.这样就可以在不编写Java代码的情况下实现一个下拉选择框. 1.在布局文 ...
- jsf使用spring注入的bean
jsf的后台bean中使用spring定义的service,需要使用@ManagedProperty,并且要具有该属性的getter/setter方法. package cn.catr.lm.idc. ...
- NSdata 与 NSString,Byte数组,UIImage 的相互转换
1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataen ...
- 第22条:理解NSCopying协议
如果想自定义类支持拷贝操作,那就要实现NSCopying协议(而不是复写copy方法)或 NSMutableCopying的协议. 不可变版本的拷贝: NSCopying协议,该协议只有一个方法: - ...
- javascript 第27节 jQuery选择器
下面的html需要以下2个文件: 1.style.css div,span,p { width:140px; height:140px; margin:5px; background:#aaa; bo ...
- CSS3的几个标签速记1
border-radius:CSS3圆角 语法:border-radius:25px; 椭圆边角:语法-border-radius:xx%;或者15px/100px; box-shadow ...
- Linux Terminal命令
Linux Terminal命令 1.Ctrl + a 回到命令行の「行首/head」. 2.Ctrl + e 回到命令行の「行尾/tail」, ctrl + end. 3.Ctrl + w 後向/b ...