iOS学习程序员iOS Developer

数据库

2017-04-03  本文已影响188人  Nbm

以前没时间整理这方面的知识,现在苦逼公司倒闭了,整理一下,巩固知识,和需要的朋友一起学习...

好了,开怼.

一 .几种数据存储方式

iOS中数据存储的方式有以下几种:

二. 处理数据的软件(Navicat)

1.点击左上角是的连接,创建一个SQLite表


Snip20170401_1.png
2.创建属性 Snip20170401_3.png
Snip20170401_5.png 增删改查大家可以自己摸索一下.

三.简单操作

DDL: 简单约束

CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, age INTEGER);
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER DEFAULT 1);

DML:(数据的增删改)约束条件

/*插入数据*/
 INSERT INTO t_student(age, score, name) VALUES ('28', 100, 'jonathan');
 INSERT INTO t_student(name, age) VALUES ('lee', '28');
 INSERT INTO t_student(score) VALUES (100);
/*删除数据*/
DELETE FROM t_student; //这是删除所有数据
DELETE FROM t_student WHERE name = 'cnw' age = 20;
DELETE FROM t_student WHERE name = 'qlt' and age >26 and score < 80;
/*修改指定数据*/
UPDATE t_student SET name = 'MM' WHERE age = 10;
UPDATE t_student SET name = 'WW' WHERE age is 7;
UPDATE t_student SET name = 'XXOO' WHERE age < 20;
UPDATE t_student SET name = 'NNMM' WHERE age < 50 and score > 10;

DQL:查询数据

/*查询*/
SELECT name, age, score FROM t_student;
SELECT * FROM t_student;
/*分页*/
SELECT * FROM t_student ORDER BY id ASC LIMIT 30, 10;
// id 升序排列,跳过30条数据取10条

四 . FMDB

1 创建表

 // 0.获取沙盒地址
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接路径
    NSString * filePath = [path stringByAppendingPathComponent:@"person.sqlite"];
    
    // 1. 加载数据库对象
    self.db = [FMDatabase databaseWithPath:filePath];
    // 2.打开数据库
    if ([self.db open]) {
        // 创建表(在FMDB框架中,增删改查销毁都统称为更新)
      BOOL success = [self.db executeUpdate:@"create table if not exists t_person (id integer primary key autoincrement,name text not null,age integer,weight real default 120);"];
        if (success) {
            NSLog(@"创表成功");
        }else{
            NSLog(@"创表失败");
        }
    }else{
        NSLog(@"打开失败");
    }

2 插入数据

- (IBAction)insert {
    // 在FMDB中可以用?当做占位符,但是:如果使用占位符,以后只能给占位符传递对象
  BOOL success = [self.db executeUpdate:@"insert into t_person(name ,age,weight) values(?,?,?);",@"QLT",@"25",@"108"];
    if (success) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

3 删除数据

- (IBAction)deleate {
    BOOL success = [self.db executeUpdate:@"delete from t_person"];
    if (success) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }

}

4 修改数据

BOOL success = [self.db executeUpdate:@"update t_person set name = 'qlt' where weight = 108"];
    if (success) {
        NSLog(@"修改成功");
    }else{
        NSLog(@"修改失败");
    }


5 查询数据

// FMResultSet结果集
    FMResultSet * set = [self.db executeQuery:@"select id,name,age,weight from t_person;"];
    if ([set next]) { // next 返回yes说明有数据
        int ID = [set intForColumnIndex:0];
        NSString * name = [set stringForColumnIndex:1];
        double weight = [set doubleForColumnIndex:3];
        NSLog(@"id = %d,name = %@,weight = %.1f",ID,name,weight);
    }else{
        NSLog(@"查询出错");
    }


FMDatabaseQueue 比较方便使用,

 // 1.创建一个FMDatabaseQueue对象
    // 只要创建数据库队列对象, FMDB内部就会自动给我们加载数据库对象
    self.queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
    
    //2 .执行操作
    // 会通过block传递队列中创建好的数据库给我们
    [self.queue inDatabase:^(FMDatabase *db) {
        // 编写需要执行的代码
        BOOL success = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, score REAL DEFAULT 1);"];
        if (success) {
            NSLog(@"创建表成功");
        }else
        {
            NSLog(@"创建表失败");
        }
    }];
- (IBAction)update {

    [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        [db executeUpdate:@"UPDATE t_person SET weight = 1500 WHERE name = 'zs';"];
        
        NSArray *array = @[@"abc"];
        array[1];
        
        [db executeUpdate:@"UPDATE t_person SET weight = 500 WHERE name = 'ls';"];
    }];

}

[self.queue inDatabase:^(FMDatabase *db) {
        // FMResultSet结果集, 结果集其实和tablevivew很像
        FMResultSet *set = [db executeQuery:@"SELECT id, name, score FROM t_student;"];
        while ([set next]) { // next方法返回yes代表有数据可取
            int ID = [set intForColumnIndex:0];
            //        NSString *name = [set stringForColumnIndex:1];
            NSString *name = [set stringForColumn:@"name"]; // 根据字段名称取出对应的值
            double score = [set doubleForColumnIndex:2];
            NSLog(@"%d %@ %.1f", ID, name, score);
        }
    }];

上一篇下一篇

猜你喜欢

热点阅读