数据库

{
   // 用来保存数据库的对象地址
   sqlite3 *dbPoint;
}
// 为了保证当前的数据库在工程里是唯一的
+ (dataBaseTool *)shareDataBaseTool;

+ (dataBaseTool *)shareDataBaseTool
{
    static dataBaseTool *tool;
    static dispatch_once_t oneToken;
    disPatch_once(&oneToken, ^{
        tool = [[dataBaseTool alloc] init];
    }];   
    return tool; 
}

// 打开数据库
- (void)openDB;

- (void)openDB
{
    // 数据库文件也保存在沙盒里的documents文件里, 所以我们先找到沙盒文件
    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(@"数据库打开失败");
    }   
}

// 给数据库创建张表格, table
- (void)createTable;

- (void)createTable
{
    // primary key 是主键的意思, 主键在当前表里数据是唯一的, 不能重复, 可以唯一的标示表数据, 一般是整数
    // autoincrement 自增, 为了主键不重复, 会采用自增的方式
    // if not exists如果没有表才会创建, 防止覆盖之间的数据
    // 数据库问题90%是sql语句出问题, 所以先保证语句没有问题, 再放到工程里使用
    NSString *sqlStr = @"create table if not exists stu(number integer primary key autoincrement, name text, sex text, age integer, hobby text)"
    // 执行这条sql语句
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if(result == SQLITE_OK) {
        NSLog(@"表创建成功");
    } else {
        NSLog(@"表创建失败");
    }
}


// 插入一个学生数据

- (void)insertStu:(Student *)stu;

- (void)insertStu:(Student *)stu
{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into stu(name, age, sex, hobby) value('%@', @'%ld', @'%@', @'%@')", stu.name, stu.age, stu.sex, stu.hobby];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF*String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
}

// 更新表里的学生数据
- (void)updataStu:(Student *)stu;

- (void)updataStu:(Student *)stu
{
    NSString *sqlStr = [NSString stringWithFormat:@"upDate stu set name = '%@', sex = '%@', hobby = '%@', age = %ld where name = '%@'", stu.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)deleteStu:(Student *)stu;

- (void)deleteStu:(Student *)stu
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from stu where name = '%@'", stu.name];
    int result = sqlite3_exec(dbPoint [UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

// 查询表里的学生数据
- (NSMutableArray *)selectALLStu;

- (NSMutableArray *)selectALLStu
{
    // 查询逻辑
    // 1. 先从本地数据库中读取表里的所有数据
    // 2. 然后逐条进行读取, 对model进行赋值
    // 3. 把已经赋值好的model放到数组中, 并且返回

    NSString *sqlStr = @"select * from stu";
    // 在语句里*是通配符的意思, 通过一个*相当于代替了表里所有的字段名
    // 接下来需要定义一个跟随指针, 它用来遍历数据库中的每行数据
    // 第三个参数: 查询语句字符限制, -1 是没有限制的

    sqlite3_stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    // 这个方法相当于把数据和跟随指针相关联, 一同完成查询功能
    // 初始化一个用来装学生的数组
    NSMutableArray *stuArr = [NSMmtableArray 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);
        // 把这列里的数据在进行类型的转换
        NSInteger StuAge = age;
        NSString *stuName = [NSString stringWithUTF8String:(const char *)name];
        NSString *stuSex = [NSString stringWithUTF8String:(const char *)sex];
        NSString *stuHobby = [NSString stringWithUTF8string:(const char *)hobby];
        // 给对象赋值, 然后把对象放到数组里
        Student *stu = [[Student alloc] init];
        stu.name = stuName;
        stu.hobby = stuHobby;
        stu.sex = stuSex;
        stu.age = stuAge;
        [stuArr addObject:stuName];
        [stu release];
        }
    } else {
        NSLog(@"查询失败%d", result);
    }
    return stuArr;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值