iOS SQLite 2015.10.30
2015-10-30 11:04
316 查看
在iOS中实现SQLite数据库的操作:1.导入框架(libsqlite3.0.tbd) 2.导入头文件<sqlite3.h> 3.实现数据的增删改查
实现简单 SQLite数据库操作 的 demo 具体过程:
1.创建名为 SQLite_Manage 的.h .m 文件,导入头文件 <sqlite3.h>
2.数据库在一个app中只有一个,使用单例模式:(代码如下)
3.打开数据库,代码如下:
4.创建表,代码如下:
5.插入数据,代码如下:
6.修改数据,代码如下:
7.查询数据,代码如下:
8.删除数据,代码如下:
9.关闭数据库,代码如下:
实现简单 SQLite数据库操作 的 demo 具体过程:
1.创建名为 SQLite_Manage 的.h .m 文件,导入头文件 <sqlite3.h>
2.数据库在一个app中只有一个,使用单例模式:(代码如下)
1 + (SQLite_Manager *)sharedManager{ 2 static SQLite_Manager *manager = nil; 3 static dispatch_once_t onceToken; 4 dispatch_once(&onceToken, ^{ 5 manager = [[SQLite_Manager alloc]init]; 6 }); 7 return manager; 8 }
3.打开数据库,代码如下:
1 - (void)open{ 2 //document路径 3 NSString *docment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 4 //sqlite 路径 5 NSString *sqlitePath = [docment stringByAppendingPathComponent:@"database.sqlite"]; 6 //打开数据库 7 int result = sqlite3_open(sqlitePath.UTF8String, &db); 8 //判断数据库是否打开成功 9 if (result == SQLITE_OK) { 10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 11 [alertView show]; 12 }else { 13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 14 [alertView show]; 15 } 16 }
4.创建表,代码如下:
1 - (void)creatTable{ 2 //sql语句 3 NSString *sqlString = @"create table Person (id integer primary key,name text,age integer)"; 4 //执行SQL语句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 8 //判断是否出现了错误 9 if (error == nil){ 10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 11 [alertView show]; 12 }else { 13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 14 [alertView show]; 15 } 16 }
5.插入数据,代码如下:
1 - (void)insert{ 2 //sql语句 3 NSString *sqlString = @"insert into Person ('name','age') values ('Ager',18)"; 4 //执行SQL语句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 //判断是否出现了错误 8 if (error == nil){ 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入数据成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 10 [alertView show]; 11 }else { 12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入数据失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 13 [alertView show]; 14 } 15 16 }
6.修改数据,代码如下:
1 - (void)update{ 2 //sql语句 3 NSString *sqlString = @"update Person set 'name' = 'Arun' where id = 1"; 4 //执行sql语句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 8 //判断是否出现了错误 9 if (error == nil){ 10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"数据更新成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 11 [alertView show]; 12 }else { 13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"数据更新失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 14 [alertView show]; 15 } 16 }
7.查询数据,代码如下:
1 - (void)select{ 2 //sql语句 3 NSString *sqlString = @"select * from Person"; 4 //准备sql 5 sqlite3_stmt *stmt = nil; 6 sqlite3_prepare(db, sqlString.UTF8String,-1, &stmt, nil); 7 //单步执行语句 8 while (sqlite3_step(stmt) == SQLITE_ROW) { 9 int ID = sqlite3_column_int(stmt, 0); 10 const unsigned char *name = sqlite3_column_text(stmt, 1); 11 int age = sqlite3_column_int(stmt, 2); 12 NSLog(@"%d,%s,%d",ID,name,age); 13 } 14 sqlite3_finalize(stmt); 15 }
8.删除数据,代码如下:
1 - (void)deleteData{ 2 //sql语句 3 NSString *sqlString = @"delete from Person where id = 1"; 4 //执行sql语句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 //判断是否出现了错误 8 if (error == nil){ 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 10 [alertView show]; 11 }else { 12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 13 [alertView show]; 14 } 15 }
9.关闭数据库,代码如下:
1 - (void)close{ 2 //关闭数据库 3 int result = sqlite3_close(db); 4 //判断数据库是否关闭成功 5 if (result == SQLITE_OK) { 6 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 7 [alertView show]; 8 }else { 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; 10 [alertView show]; 11 } 12 }
相关文章推荐
- SQL常遇到的小问题整理
- mysql 字符串和数值比较
- 数据库的恢复模式
- MySQL 加锁处理分析()
- Oracle日期时间函数大全
- redis+tomcat7实现session共享
- MySQL Study之--MySQL innodb引擎备份工具XtraBackup之三(数据库增量备份)
- Oracle 差异增量和累计增量备份
- 11g单实例数据库打最新11.2.0.4.8 PSU补丁记录
- MySQL Study之--MySQL innodb引擎备份工具XtraBackup之三(数据库增量备份)
- oracle中exp,imp的使用详解
- Oracle 手动创建数据库步骤详解
- mysql5.7.9正式版安装
- MongoDB 学习笔记五 MapReduce
- MySql concat函数
- 项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案
- PgSQL Replication Slot 实现分析
- Navicat Premium 连接 Oracle 数据库 (续)
- oracle数据迁移中的故障解决:
- mysql中or和in的效率问题