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 ...
随机推荐
- sscanf、strsep
#include <stdio.h> #include <string.h> int main() { char token[] ="abdzxbcdefgh&quo ...
- mig_ddr4_ultrascale
http://china.xilinx.com/support/documentation-navigation/design-hubs/dh0061-ultrascale-memory-interf ...
- angularjs的一些优化小技巧
尽可能少调用 ng-repeat ng-repeat默认会创建很多监听器,所以在数据量很大的时候,这个非常消耗页面性能,我觉的只有在当需要经常更新数据列表的时候才需要用ng-repeat,要不然放那么 ...
- Anddoi 将时间转换为指定时区的时间
import java.text.Format;import java.text.ParseException;import java.text.SimpleDateFormat;import jav ...
- php里ezpdo orm框架初探
http://jackyrong.iteye.com/blog/238930 http://www.oschina.net/project/tag/126/orm?sort=view&lang ...
- 2) LINQ编程技术内幕--yield return
yield return 使用.NET的状态机生成器 yield return关键词组自动实现IDisposable,使用这个可枚举的地方, 还存在一个隐含的try finally块. 示例代码: c ...
- css 盒子模型理解
盒子模型是html+css中最核心的基础知识,理解了这个重要的概念才能更好的排版,进行页面布局.下面是自己积累和总结的关于css盒子模型的知识^_^,希望对初学者有用. 一.css盒子模型概念 CSS ...
- forever start Error: Cannot find module './daemon.v0.10.26'
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3590158.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- mysql密码忘记后重置密码
之前在centOS里安装了xampp,设置了mysql数据库root密码,今天需要增加个数据库,发现忘记之前设置的密码是什么了.经过一番摸爬滚打,终于搞明白了,注意以下的操作都是以linux的root ...
- 用C#开发一个WinForm版的批量图片压缩工具
我们在实际项目开发过程中,曾经遇到过一个需求,就是要开发一个对大量图片进行整理(删除掉一些不符合要求的图片).归类(根据格式进行分类,比如jpg格式.bmp格式等).压缩(因为有的图片很大很占空间,看 ...