iOS UI19_数据库
2015-08-25 08:37
411 查看
// // Student.h // UI19_数据库 // // Created by dllo on 15/8/24. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import <Foundation/Foundation.h> @interface Student : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *sex; @property(nonatomic,copy)NSString *hobby; @property(nonatomic,assign)NSInteger age; @end
// // dataBaseTool.h // UI19_数据库 // // Created by dllo on 15/8/24. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import <Foundation/Foundation.h> #import <sqlite3.h> #import "Student.h" @interface dataBaseTool : NSObject { //用来保存数据库对象的地址 sqlite3 *dbPoint; } //为了保证当前数据库在工程里是唯一的,我们用单例方式创建一个数据库工具对象 +(dataBaseTool *)shareDataBaseTool; //打开数据库 -(void)openDB; //给数据库创建张表格,table -(void)createTable; //插入一个学生 -(void)insertStu:(Student *)stu; //更新表里的学生 -(void)updateStu:(Student *)stu; //删除表里学生 -(void)deleteStu:(Student *)stu; //查询数据库中所有学生表里的数据 -(NSMutableArray *)selectAllStu; @end
// // dataBaseTool.m // UI19_数据库 // // Created by dllo on 15/8/24. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import "dataBaseTool.h" @implementation dataBaseTool +(dataBaseTool *)shareDataBaseTool { static dataBaseTool *tool; static dispatch_once_t oneToKen; dispatch_once(&oneToKen, ^{ tool = [[dataBaseTool alloc] init]; }); return tool; } -(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(@"打印失败"); } } -(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{ //语句里值的位置要加上单引号 NSString *sqlStr = [NSString stringWithFormat:@"insert into stu (name, age, sex, hobby) values ('%@', '%ld', '%@', '%@')", stu.name, stu.age, stu.sex, stu.hobby]; //执行sql语句 int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil); if (result == SQLITE_OK) { NSLog(@"添加成功"); } else { NSLog(@"添加失败"); } } -(void)updateStu:(Student *)stu { NSString *sqlStr = [NSString stringWithFormat:@"update stu set sex = '%@', hobby = '%@', age = '%ld' where name = '%@' ", stu.sex, stu.hobby, stu.age, stu.name]; //执行sql int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil); NSLog(@"%@", sqlStr); if (result == SQLITE_OK) { NSLog(@"更新成功"); } else { NSLog(@"更新失败"); NSLog(@"%d",result); } } -(void)deleteStu:(Student *)stu { NSString *sqlStr = [NSString stringWithFormat:@"delete from stu where name ='%@' ",stu.name]; //执行sql int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil); NSLog(@"%@", sqlStr); if (result == SQLITE_OK) { NSLog(@"删除成功"); } else { NSLog(@"删除失败"); NSLog(@"%d",result); } } -(NSMutableArray *)selectAllStu { //查询的逻辑 //1.先从本地的数据库中读取某张表里的所有数据 //2.然后逐条读取,对model进行赋值 //3.把已经赋值好的model放到数组中,并且返回 NSString *sqlStr = @"select * from stu"; //在语句里*是通配符的意思,通过一个*相当于代替了表里所有的字段名 //接下来需要定义一个跟随指针,他用来遍历数据库表中的每行数据 sqlite3_stmt *stmt = nil; //第三个参数:查询语句字数限制,-1是没有限制 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); //把列里的数据在进行类型的转换 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.age =stuAge; stu.sex=stuSex; [stuArr addObject:stu]; [stu release]; } } else { NSLog(@"查询失败"); NSLog(@"%d",result); } return stuArr; } @end
相关文章推荐
- UI13_数据解析XML,JSON
- (素材源码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象PS有惊喜
- (素材源码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象牛逼Demo
- 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象
- Leetcode: Unique Binary Search Trees II
- [poj2778 DNA Sequence]AC自动机,矩阵快速幂
- UISearchbar 搜索按钮更换 代码实现UISearchbar去除第一响应
- [LeetCode#128]Longest Consecutive Sequence
- HDU 5400 Arithmetic Sequence(数学,容斥)
- uva 120 Stacks of Flapjacks(练习deque)
- UI 常用方法总结之--- UITableView
- php生成guid
- iOS 坑点 Assertion failure in - UITableView dequeueReusableCellWithIdentifier:forIndexPath:
- UIActivityViewController
- requirejs实现模块化
- 面试题之:String,StringBuffer与StringBuilder的区别?
- Mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- UI_UIViewController_视图控制器(VC)
- UI029---UITableView中加载数据
- Example to use django queryset