使用一个单例来做操作数据库的调用者.
首先, 需要去Build Phases 的 Link Binary With Libraries 添加libsqlite3.0
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "Student.h"
@interface dataBase_Tool : NSObject
{
// 用来保存数据库对象的地址.
// 现在这个成员变量是私有的,也没有getter.setter方法. 外界找不到,改不了.
sqlite3 *dbPoint;
}
// 为了保证当前数据库在工程中是唯一的, 我们使用单里的创建方式创建一个数据库对象
+ (DataBase_Tool *)shareDataBaseTool;
// 数据库保存的位置,也在document
// 打开数据库
- (void)openDB;
// 创建一张表格, table
- (void)createTable;
// 插入一个学生对象信息.
- (void)insertStudent:(Student *)stu;
// xiugai
- (void)upDataStu:(Student *)stu;
- (void)deleteDataWithStuName:(Student *)stu;
- (NSMutableArray *)selectAllStu;
- (void)closeDB;
这些方法的内部实现:
#import "dataBase Tool.h"
@implementation dataBase_Tool
+ (dataBase_Tool *)shareDataBaseTool{
static dataBase_Tool *dataTool;
static dispatch_once_t oneToKen;
dispatch_once(&oneToKen, ^{
dataTool = [[dataBase_Tool alloc] init];
});
return dataTool;
}
// 数据库保存的位置,也在document
// 打开数据库
- (void)openDB
{
// 先找沙盒路径
NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sandBoxPath = sandBox[0];
// 拼接文件路径,系统会根据这个路径查找,如果有对应文件就直接打开数据库,如果没有就会创建一个数据库.
NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Student.sqlite"];
int result = sqlite3_open([documentPath UTF8String], &dbPoint);
if (result == SQLITE_OK) {
NSLog(@"数据库开启");
NSLog(@"%@",documentPath);
}else{
NSLog(@"数据库打开失败");
}
}
- (void)createTable
{
//primary key 是主键的意思,主键在当前表中数据是唯一的,不能重复,可以唯一表示一条数据, 一般是整数.
//autoincrement 自增,为了让主键不重复,会让主键采用自增的方式.
// if not exists 如果没有表才会创建,防止重复创建覆盖之前的数据.
// 数据库问题90%是sqlite语句出问题,所以先保证语句没问题,再放在工程中使用.
NSString *sqlStr = @"create table if not exists stu(number integer primary key autoincrement, name text, sex text, age integer, hobby text)";
// 执行这条sqlite语句
int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"表创建成功!");
}else {
NSLog(@"表创建失败!");
}
}
- (void)insertStudent:(Student *)stu{
// 语句里值的位置加上单引号
NSString *sqlStr = [NSString stringWithFormat:@"insert into stu (name,age,hobby,sex)values ('%@','%ld','%@','%@')",stu.name,stu.age,stu.hobby,stu.sex];
// 执行sqlite语句
int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"添加成功!");
}else{
NSLog(@"添加失败!");
}
}
- (void)upDataStu:(Student *)stu{
NSString *sqlStr = [NSString stringWithFormat:@"update stu set sex= '%@' , hobby = '%@' ,age = %ld where name = '%@'",stu.sex, stu.hobby, stu.age, stu.name];
int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"更新成功");
}else{
NSLog(@"更新失败! 原因:%d",result);
}
}
- (void)deleteDataWithStuName:(Student *)stu{
NSString *sqlStr = [NSString stringWithFormat:@"delete from stu where name = '%@'",stu.name];
int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败!");
}
}
- (NSMutableArray *)selectAllStu{
// 查询的逻辑
// 1.先从本地的数据库中读取某张表里的所有数据
// 2.然后逐条进行读取,对model进行赋值
// 3.把已经赋值好的model放在数组中,并且返回
NSString *SqlStr = @"select * from stu";
// 在语句中*是通配符的意思, 通过一个*相当于代替了表里的所有的字段名.
// 接下来需要定义一个跟随指针,他用来遍历表中的每行数据
// 第三个参数: 查询语句字数限制, -1 是没有限制
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(dbPoint, [SqlStr UTF8String], -1, &stmt, nil);
// 这个方法相当于把数据库和跟随指针相关联,一同完成查询.
// 初始化一个用来装学生的数组
NSMutableArray *stuArr = [NSMutableArray array];
if (result == SQLITE_OK) {
NSLog(@"查询成功");
// 开始遍历查询数据库的每一行数据
// 让跟随指针进行遍历查询,如果没有行了,才会停止循环
// 满足条件, 则逐列读取这一行上的数据.
while (sqlite3_step(stmt) == SQLITE_ROW) {
// 第二个参数代表的是当前这列数据在表的第几列.
const unsigned char *name = sqlite3_column_text(stmt, 1);
const unsigned char *sex = sqlite3_column_text(stmt, 2);
int age = sqlite3_column_int(stmt, 3);
const unsigned char *hobby = sqlite3_column_text(stmt, 4);
// 把列里面的数据进行类型转换
NSString *stuName = [NSString stringWithUTF8String:(const char *)name];
NSInteger stuAge = age;
NSString *stuSex = [NSString stringWithUTF8String:(const char *)sex];
NSString *stuHobby = [NSString stringWithUTF8String:(const char *)hobby];
// 把对象赋值, 然后把对象放在数组中
Student *stu = [[Student alloc] initWithName:stuName sex:stuSex hobby:stuHobby age:stuAge];
[stuArr addObject:stu];
}
}else{
NSLog(@"查询失败");
NSLog(@"%d",result);
}
return stuArr;
}
- (void)closeDB{
int result = sqlite3_close(dbPoint);
if (result == SQLITE_OK) {
NSLog(@"数据库关闭成功!");
}else{
NSLog(@"数据库关闭失败!");
}
}