数据库操作语句

CoreData

CoreData的使用步骤

- 创建模型文件
- 添加实体
- 创建实体类
- 生成上下文件 关联模型文件生成数据库
- 保存对象到数据库
- 从数据库获取对象
- 更新数据
- 删除数据

数据持久化:是将一个数据保存到文件中,而不是内存中

CoreData简单用法

/**
 1. 创建模型文件
 2. 添加实体
 3. 创建实体类(就是一张表)
 4. 生成上下文件 关联模型文件生成数据库
 */

- (void)setUpCoreData {
   /** 关联的时候:如果本地没有这个文件,coreData会自己创建 */
  /** 生成上下文 */
  NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
  /** 模型文件 */
  NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
  /** 上下文要关联数据库 */
  /** 持久化调度器 */
  NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
  /** 告诉CoreData数据库的名字和路径 */
  NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  NSString *sqlitePath = [doc stringByAppendingString:@"company.sqlite"];

  [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];

  context.persistentStoreCoordinator = store;

  _context = context;
}


#pragma mark - 添加
- (IBAction)addEntity {
    
    Entity *enti = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:_context];
    enti.name = @"wangwu";
    enti.hight = @2.30;
    
    NSError *error = nil;
    [_context save:&error];
    
    if (error) {
        NSLog(@"%@", error);
    }
}

#pragma mark - 读取员工
- (IBAction)readEntity {
    
    /** 1. FetchRequest抓取对象 */
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
    
    /** 2. 设置过滤条件 */
    NSPredicate *predic = [NSPredicate predicateWithFormat:@"name = %@", @"wangwu"];
    request.predicate = predic;
    
    /** 3. 排序 */
    /** 身高的升序排序 */
    NSSortDescriptor *heightSort = [NSSortDescriptor sortDescriptorWithKey:@"hight" ascending:YES];
    request.sortDescriptors = @[heightSort];/*< 数组的条件排序 */
    
    
    /** 4. 执行请求 */
    NSError *error = nil;
    NSArray *arr = [_context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@", error);
    }
    
    /** 遍历数组,读取的内容 */
    for (Entity *ti in arr) {
        NSLog(@"%@, %@", ti.name, ti.hight);
    }
}

#pragma mark - 更新员工

- (IBAction)updateEntity {
    
    /** 更改张三  身高两米 */
    
    /** 1.查找张三 */
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
    
    /** 设置过滤条件 */
    NSPredicate *predic = [NSPredicate predicateWithFormat:@"name = %@", @"zhangsan"];
    request.predicate = predic;
    /** 执行请求 */
    NSArray *enti = [_context executeFetchRequest:request error:nil];
    
    /** 2.更新身高 */
    for (Entity *en in enti) {
        en.hight = @2.0;
    }
    
    /** 3.保存 */
    [_context save:nil];
}

#pragma mark - 删除
- (IBAction)delete {
    
    /** 找到list */
    /** .查找张三 */
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
    
    NSPredicate *predic = [NSPredicate predicateWithFormat:@"name = %@", @"zhangsan"];
    request.predicate = predic;
    
    NSArray *enti = [_context executeFetchRequest:request error:nil];
    
    /** 删除 */
    for (Entity *entity in enti) {
        [_context deleteObject:entity];
    }
    
    /** 保存 */
    [_context save:nil];
}

数据库软件使用中的语句

  • DDL:数据定义语句
    • 创建表格
      • create table if not exists + 表名 (字段名1 字段类型1,字段名2 字段类型2)
      • 例子:create table if not exists t_class (id integer primary auto increment, name text);
    • 删除表格
      • drop table if exists + 表名
      • DML 数据操作源(增删改查)
    • 插入
      • insert into + 表名 (字段名) values (名)
      • INSERT INTO t_class(name) VALUES ('哈哈哈哈');
    • 更改
      • update + 表名 set + 字段名 = 字段1的值
      • UPDATE t_class SET name = '哈哈哈哈’;
      • 这样的更改是所有的都改
    • 根据条件更改 (where)
      • UPDATE t_student set name = 'qq' WHERE age < 10 and id , 50;
    • 删除
      • delete from + 表名
      • DELETE FROM t_class;
    • 删除记录,不是删除表
      • DQL语句 用来查询的
    • 查询
      • SELECT name FROM t_class;
      • select * from 查询所有字段
    • 查询后排序
      • 降序:SELECT * FROM t_student ORDER BY age desc;
      • 升序:SELECT * FROM t_student ORDER BY age asc;
    • 约束 (建议给字段设定严格的约束,以保证数据的规范性)
      • not null:规定字段的值不能为null
      • unique:规定字段的值必须唯一
      • default:制定字段的默认值
        • create table t_student (id integer, name text not null unique, age integer not null default 1);
        • name字段不能为null,并且唯一
        • age字段不能为null,并且默认为1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.显示所有数据库 show databases;//展示所有数据库 2,创建数据管理系统create datab...
    richer_y阅读 516评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,765评论 18 399
  • 什么是SQLite?数据库存储数据的步骤 ●SQLite是一款轻型的嵌入式数据库,它占用资源非常的低,在嵌入式设备...
    飞行的猫阅读 2,523评论 1 7
  • 推荐:http://www.cnblogs.com/picaso/archive/2012/05/17/25058...
    郝赫阅读 3,541评论 0 4
  • 第一章 楔子 乘车时,满车的栀子花香味。厚着脸皮问师傅要了几朵,心里满满的全是开心。 六年前,我们就是因为栀子花...
    蓝凌纸鸢阅读 419评论 2 1