iOS 中SQLite数据库操作
2015-10-25 20:45
393 查看
在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中只有一个,使用单例模式:(代码如下)
+ (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,-1, &stmt, nil); //单步执行语句 while (sqlite3_step(stmt) == SQLITE_ROW) { int ID = sqlite3_column_int(stmt, 0); const unsigned char *name = sqlite3_column_text(stmt, 1); int age = sqlite3_column_int(stmt, 2); 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]; } }
相关文章推荐
- Mysql cookbook(1)
- 大型分布式数据库集群的研究
- 在一条SQL语句中向2个表中插入数据解决方案
- 黄聪:优化清理WordPress数据库wp_options表(缩小autoload体积)
- SQLite数据库总结二
- MongoDB入门篇
- 后台数据库优化——板机
- sql 存储过程 循环
- SQL查询语句大全集锦
- MsSqlServer 复制分发概述
- MySQL自带的性能压力测试工具mysqlslap详解
- MSSQLSERVER之发布-分发-订阅
- hibernate的两种数据库连接池技术
- 第6章深入使用Hibernate 6.6 SQL查询
- linux安装和配置 mysql、redis 过程中遇到的问题记录(转)
- SQL的四种连接查询
- sql语句嵌套查询
- 盲人摸象——iOS简单应用ToDoList之sqlite数据本地化
- 关系型数据库
- mysql 学习记录(十六)--优化常用sql