数据库sqlite

  • 首先导入库 libsqlite3.0.tbd
  • 做一个声明
{
sqlite3 *db;//这是指向数据库的指针,我们其他操作用这个指针来完成.
}
  • 打开数据库
- (void)openSqlite{
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [str stringByAppendingPathComponent:@"my.sqlite"];
int result = sqlite3_open([path UTF8String], &db);
if (result == SQLITE_OK){
NSLog(@"数据库打开成功");
}else{
NSLog(@"数据打开失败");
}
}
  • 创建表格
- (void)CreateTable{
//1.准备 sqlite 语句
  NSString *sqlite = [NSString stringWithFormat:@"create table if not exists 'student' ('number' integer primary key autoincrement not null,'name' text,'sex' text,'age' integer)"];
//2执行 sqlite 语句
char *error = NULL;//执行 sqlite 语句失败的时候,会把失败的原因存储到里面.
int result = sqlite3_exec(db, [sqlite UTF8String], NULL,NULL,&error);//因为 sqlite 是 C语言界别的 string 类型参数,所以需要 UTF8String 来惊醒转换.
// 判断
    if (reslut == SQLITE_OK) {
        NSLog(@"OK");
    }else{
        NSLog(@"Fail : %s",error);
    }
}
  • 添加数据的方法
- (void)addStudent:(Student *)stu{
//1.准备 sqlite 语句
    NSString *sqlite = [NSString stringWithFormat:@"insert into student (number,name,sex,age) values ('%ld','%@','%@','%ld')",stu.number,stu.name,stu.sex,stu.age];
    //2执行语句
    char *error = NULL;
    int result = sqlite3_exec(db, [sqlite UTF8String], NULL, NULL, &error);//第三个与第四个参数之所以为空,是为了以后扩展
    // 判断
    if (result == SQLITE_OK) {
        NSLog(@"OK");
    }else{
        NSLog(@"Fail : %s",error);
    }
}
  • 删除数据
//删除数据
- (void)deleteStudent:(Student *)stu{
    NSString *sqlite = [NSString stringWithFormat:@"delete from student where number = %ld",stu.number];
    char *error = NULL;
    int result = sqlite3_exec(db, [sqlite UTF8String], NULL, NULL, &error);//第一个指针,第二个参数 sqlite 二进制
    // 判断
    if (result == SQLITE_OK) {
        NSLog(@"OK");
    }else{
        NSLog(@"Fail : %s",error);
    }
}
  • 查询所有的数据
//查询所有的数据
- (NSMutableArray *)selectStudent:(Student *)stu{
    //1,准备sqlite 语句
    NSString *sqlite = [NSString stringWithFormat:@"select * from student "];
    //2,执行语句
    //伴随指针
    sqlite3_stmt *error = NULL;
    //预执行.用来判断当前的执行的语句是否正确.
    int result = sqlite3_prepare(db, [sqlite UTF8String], -1, &error, NULL);//第三个参数为一次性返回所有的数据.-1为全部返回
    //创建一个数组,用来存储数据
    NSMutableArray  *array = [[NSMutableArray alloc] init];
    
    if (result == SQLITE_OK) {
        NSLog(@"OK");
        while (sqlite3_step(error) == SQLITE_ROW) {
            Student *stu = [[Student alloc] init];
            stu.number = sqlite3_column_int(error, 0);
            stu.name = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(error, 1)];//类型不匹配,需要强转
            stu.sex = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(error, 2)];
            stu.age = sqlite3_column_int(error, 3);
            [array addObject:stu];
        }
    }else{
        NSLog(@"Fail");
    }
    //关闭伴随指针
    sqlite3_finalize(error);
    return array;
}
  • 更新数据
- (void)updateStudent:(Student *)stu{
    NSString *sqlite = [NSString stringWithFormat:@"update student set name = '%@',sex = '%@',age = '%ld' where number = %ld ",stu.name,stu.sex,stu.age,stu.number];
    char *error = NULL;
    int result = sqlite3_exec(db, [sqlite UTF8String], NULL, NULL, &error);
    
    if (result == SQLITE_OK) {
        NSLog(@"OK");
        NSLog(@"%@",sqlite);
    }else{
        NSLog(@"Fail : %s",error);
    }
}
  • 在操作完毕后,需要关闭数据库
//关闭数据库
- (void)close{
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [str stringByAppendingPathComponent:@"my.sqlite"];
    int result = sqlite3_close(db);
    if (result ==SQLITE_OK) {
        NSLog(@"关闭");
    }else{
        NSLog(@"未关闭");
    }
    db =nil;
}

你可能感兴趣的:(数据库sqlite)