iPhone中支持通过sqlite3来访问iPhone本地的数据库。
具体使用方法如下
添加开发包libsqlite3.0.dylib
首先是设置项目文件,在项目中添加iPhone版的sqlite3的数据库的开发包,在项目下的Frameworks点击右键,然后选择libsqlite3.0.dylib文件。
添加完以后我们首先来介绍一些sqlite3.0的函数
sqlite3 *pdb, 数据库句柄,跟文件句柄FILE很类似
sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句
sqlite3_open(), 打开数据库
sqlite3_exec(), 执行非查询的sql语句
sqlite3_prepare(), 准备sql语句,执行select语句或者要使用parameter bind时,用这个函数(封装了sqlite3_exec).
Sqlite3_step(), 在调用sqlite3_prepare后,使用这个函数在记录集中移动。
Sqlite3_close(), 关闭数据库文件
还有一系列的函数,用于从记录集字段中获取数据,如
sqlite3_column_text(), 取text类型的数据。
sqlite3_column_blob(),取blob类型的数据
sqlite3_column_int(), 取int类型的数据
PreparedStatement方式处理SQL请求的过程
说白了sql就是写好sql语句 然后执行sql语句
首先我们创建一个Student类
@interface Student : NSObject @property(nonatomic,strong)NSString *name; @property(nonatomic,assign)NSInteger age; @property(nonatomic,strong)NSString *number; -(instancetype)initWithName:(NSString *)name Age:(NSInteger)age Number:(NSString *)number;
-(instancetype)initWithName:(NSString *)name Age:(NSInteger)age Number:(NSString *)number{ self =[super init]; if (self) { self.name=name; self.age=age; self.number=number; } return self; }
然后我们在创建一个单例 (在单例里面写sql的创建 查询等方法)
#import <Foundation/Foundation.h> #import "Student.h" @interface DataManager : NSObject //单例 + (instancetype)shareDataManager; //创建数据库 - (void)openDB; //创建表 - (void)createTable; //向数据库中添加一个学生信息 - (void)addStudent:(Student *)student; //通过学号删除一个学生 - (void)deleteStudentWithNumber:(NSString *)number; //获取数据库中所有学生 - (NSMutableArray *)getAllStudents; //更新数据库信息 - (void)updateNameOfStudent:(NSString *)name Number:(NSString *)number; //查询所有指定姓的学生 - (NSMutableArray *)queryStudentBySurame:(NSString *)surname; //关闭数据库 - (void)closeDB; @end
#import "DataManager.h" #import <sqlite3.h> @implementation DataManager { sqlite3 *_db;//表示数据库的链接 } //单例 + (instancetype)shareDataManager{ static DataManager *manager =nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ manager =[[DataManager alloc]init]; }); return manager; } #pragma mark---创建数据库 - (void)openDB{ //获取沙盒路径 NSString *documentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject; //拼接文件存放路径 NSString *dbPath=[documentsPath stringByAppendingPathComponent:@"student.db"]; //创建一个数据库并连接 int result= sqlite3_open(dbPath.UTF8String, &_db); if (result ==SQLITE_OK) { NSLog(@"创建数据库成功!"); }else{ NSLog(@"创建数据库失败!"); } } #pragma mark---创建表 - (void)createTable{ //1.准备创建表的sql语句 NSString * createSql=@"CREATE TABLE student(name TEXT,age INTEGER,number TEXT PRIMARY KEY)"; //执行sql语句 [self excuteSql:createSql Event:@"创建表"]; } #pragma mark----向数据库中添加一个学生信息 - (void)addStudent:(Student *)student{ //准备SQL语句 NSString * sql=[NSString stringWithFormat:@"insert into student(name,age,number) values('%@','%lu','%@')",student.name,student.age,student.number]; //执行sql语句 [self excuteSql:sql Event:@"添加学生"]; } #pragma mark-----通过学号删除一个学生 - (void)deleteStudentWithNumber:(NSString *)number{ //准备sql语句 NSString *deleteSql =[NSString stringWithFormat:@"delete from student where number='%@'",number]; //执行sql语句 [self excuteSql:deleteSql Event:@"删除学生"]; } #pragma mark ------获取数据库中所有学生 - (NSMutableArray *)getAllStudents{ //准备sql语句 NSString *querySql =@"select * from student"; //执行sql语句 NSMutableArray * array=[self quereySql:querySql Event:@"获取所有学生"]; return array; } #pragma mark----更新数据库信息 - (void)updateNameOfStudent:(NSString *)name Number:(NSString *)number{ //准备sql语句 NSString * updateSql =[NSString stringWithFormat:@"update student set name='%@'where number='%@'",name,number]; //执行sql语句 [self excuteSql:updateSql Event:@"更新数据库"]; } #pragma mark---查询所有指定姓的学生 - (NSMutableArray *)queryStudentBySurame:(NSString *)surname{ //准备sql语句 NSString * querySql=[NSString stringWithFormat:@"select *from student where name like '%@%%'",surname]; //执行sql语句 NSMutableArray *studentArray =[self quereySql:querySql Event:@"查找特定学生"]; return studentArray; } #pragma mark----关闭数据库 - (void)closeDB{ int result =sqlite3_close(_db); if (result==SQLITE_OK) { NSLog(@"关闭数据库成功!"); }else{ NSLog(@"关闭数据库失败!"); } } //执行sql语句并获取所有学生 - (NSMutableArray *)quereySql:(NSString *)sql Event:(NSString *)event{ sqlite3_stmt *stmt; int result=sqlite3_prepare_v2(_db, sql.UTF8String, -1, &stmt, NULL); //检查sql语句是否正确 if (result==SQLITE_OK) { //创建学生数组 NSMutableArray * studentsArray=[NSMutableArray array]; //查询到一条数据 while (SQLITE_ROW==sqlite3_step(stmt)) { NSString * number =[NSString stringWithUTF8String:(const char*)sqlite3_column_text(stmt, 2)]; NSInteger age=sqlite3_column_int(stmt, 1); NSString *name =[NSString stringWithUTF8String:(const char*)sqlite3_column_text(stmt, 0)]; //封装model对象 Student * student =[[Student alloc]initWithName:name Age:age Number:number]; //将学生给你对象添加都数组中 [studentsArray addObject:student]; NSLog(@"%@成功",event); //释放stmt sqlite3_finalize(stmt); return studentsArray; } }else{ //释放stmt sqlite3_finalize(stmt); NSLog(@"%@失败",event); } return nil; } //执行sql语句 - (void)excuteSql:(NSString *)sql Event:(NSString *)event{ int result =sqlite3_exec(_db, sql.UTF8String, NULL, NULL, NULL); if (result==SQLITE_OK) { NSLog(@"%@成功!",event); }else{ NSLog(@"%@失败",event); } } @end
最后在viewController里执行这些方法
//打开数据库 [[DataManager shareDataManager]openDB]; //创建数据库表 [[DataManager shareDataManager] createTable]; //添加一个学生信息到数据库中 Student *stu1=[[Student alloc]initWithName:@"wangbin" Age:24 Number:@"100"]; Student * stu2=[[Student alloc]initWithName:@"fengqian" Age:22 Number:@"001"]; Student * stu3=[[Student alloc]initWithName:@"huxing" Age:22 Number:@"111"]; Student * stu4=[[Student alloc]initWithName:@"zhangsan" Age:27 Number:@"45"]; Student * stu5=[[Student alloc]initWithName:@"zhaoliu" Age:11 Number:@"4"]; //添加学生 [[DataManager shareDataManager]addStudent:stu1]; [[DataManager shareDataManager]addStudent:stu2]; [[DataManager shareDataManager]addStudent:stu3]; [[DataManager shareDataManager]addStudent:stu4]; [[DataManager shareDataManager]addStudent:stu5]; //查询所有学生 NSArray *array= [[DataManager shareDataManager]getAllStudents]; Student * stu=array[0]; NSLog(@"name=%@,age=%lu,number=%@",stu.name,stu.age,stu.number); //查询名叫fengqian的学生 NSArray * array1 =[[DataManager shareDataManager]queryStudentBySurame:@"fengqian"]; Student * stu0 =array1[0]; NSLog(@"%lu",array1.count); NSLog(@"name=%@,age=%lu,number=%@",stu0.name,stu0.age,stu0.number); //根据学号删除学生 [[DataManager shareDataManager]deleteStudentWithNumber:@"100"]; //关闭数据库 [[DataManager shareDataManager]closeDB];