Xcode使用sql语句

2015-12-29  本文已影响677人  pingui

数据库: 数据的集散地,有效的存储和管理数据

关系型数据库:1. 用二维表组织数据; 2. 结构化查询语言(SQL - Structured Query Language)

行:一条记录
列:一个字段
主键:能够唯一标识一条记录的字段
外键:其他表的主键(外来的主键)

SQL语句

SQL全称结构化查询语言,是数据库的编程语言,可以分为DDL(数据定义语言)、DML(数据操纵语言)、DQL(数据查询语言)和DCL(数据控制语言)。

说明: 不区分大小写

先创建一张学生表

格式: 名称 类型 约束

create table TbStudent
(
stuid integer primary key, --学号, 整型,作为主键(不能重复)
stuname varchar(20) not null, -- 姓名,可变字符串,要求非空
stusex char(1) default '男', --性别,字符串,默认男
stuaddr varchar(50) --家庭住址,可变字符串
);

下面通过例子来说明sqlite的使用##

创建一张部门表

create table TbDept
(
deptno tinyint primary key,
dname varchar(10) not null,
dloc varchar(20) not null
);

插入数据

insert into TbDept values (10, '会计部', '北京');
insert into TbDept values (20, '研发部', '成都');
insert into TbDept values (30, '销售部', '重庆');
insert into TbDept values (40, '运维部', '深圳');

创建一张员工表

create table TbEmp
(
empno int primary key,
ename varchar(20) not null,
job varchar(10) not null,
mgr int,
sal int not null,
dno tinyint,
foreign key (dno) references TbDept(deptno)
);

插入数据

insert into TbEmp values (7800, '张三丰', '总裁', null, 9000, 20);
insert into TbEmp values (2056, '乔峰', '分析师', 7800, 5000, 20);
insert into TbEmp values (3088, '李莫愁', '设计师', 2056, 3500, 20);
insert into TbEmp values (3211, '张无忌', '程序员', 2056, 3200, 20);
insert into TbEmp values (3233, '丘处机', '程序员', 2056, 3400, 20);
insert into TbEmp values (3251, '张翠山', '程序员', 2056, 4000, 20);
insert into TbEmp values (5566, '宋远桥', '会计师', 7800, 4000, 10);
insert into TbEmp values (5234, '郭靖', '出纳', 5566, 2000, 10);
insert into TbEmp values (3344, '黄蓉', '销售主管', 7800, 3000, 30);
insert into TbEmp values (1359, '胡一刀', '销售员', 3344, 1800, 30);
insert into TbEmp values (4466, '苗人凤', '销售员', 3344, 2500, 30);
insert into TbEmp values (3244, '欧阳锋', '程序员', 3088, 3200, 20);
insert into TbEmp values (3577, '杨过', '会计', 5566, 2200, 10);
insert into TbEmp values (3588, '朱九真', '会计', 5566, 2500, 10);

完成下面的练习

1.查询最高工资及其对应员工

select ename, max(sal) from TbEmp;

select ename, sal from TbEmp where sal=(select max(sal) from TbEmp);

2.计算每位员工的年薪

select ename, sal*12 from TbEmp;

3.统计有员工部门的人数

select dno, count(dno) from TbEmp group by dno;

select dname, count(dno) from TbEmp, TbDept where deptno = dno group by dno;

4.求挣最高薪水的员工(boss除外)的姓名

select ename, max(sal) from TbEmp where mgr is not null;

select ename, sal from TbEmp where sal=(select max(sal) from TbEmp where mgr is not null);

5.查询薪水超过平均薪水的员工的姓名和工资

select ename, sal from TbEmp where sal > (select avg(sal) from TbEmp);

6.查询薪水超过其所在部门平均薪水的员工的姓名、部门编号和工资

select ename, deptno, sal from TbEmp, TbDept where deptno = dno and (sal > (select avg(sal) from TbEmp group by dno));

select ename, t1.dno, sal from TbEmp as t1,
    (select dno, avg(sal) as avgSal from TbEmp group by dno) as t2 where    t1.dno=t2.dno and t1.sal>t2.avgSal;

select ename, t1.dno, sal from TbEmp as t1 inner join
    (select dno, avg(sal) as avgSal from TbEmp group by dno) as t2 on 
    t1.dno=t2.dno and t1.sal>t2.avgSal;

7.查询部门中薪水最高的人姓名、工资和所在部门名称

select ename, max(sal), dname from TbEmp, TbDept where deptno = dno group by dno ;

8.哪些人是主管

select ename, job from TbEmp where empno in (select distinct mgr from TbEmp where mgr is not null);

select ename as '主管' from TbEmp where empno in (select distinct mgr from TbEmp);

9.求平均薪水最高的部门的名称和平均工资

select dname, avg(sal) as avg1 from TbEmp, TbDept where deptno = dno  group by dno order by avg1 desc limit 1 offset 0;

10.求薪水最高的前3名雇员

select ename, sal from TbEmp order by sal desc limit 3;

select ename, sal from TbEmp where mgr is not null order by sal desc limit 3 offset 0;

11.求薪水排在第4-6名雇员

select ename, sal from TbEmp order by sal desc limit 3 offset 3;

select ename, sal from TbEmp where mgr is not null order by sal desc limit 3 offset 3;

12.求薪水最低的部门经理所在部门的名称

select ename, dname, max(sal) as max1 from TbEmp, TbDept where deptno = dno group by dno order by max1 desc limit 1 offset 2;

如何在Xcode中使用sql语句

说明: 这里是将已经创建好的数据库文件导入项目中

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 用户数据必须保存在Documents文件夹中
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/company.db"];
    // 打印沙箱路径
    //NSLog(@"%@",NSHomeDirectory());
    
    // 判断应用程序沙箱中是否有数据库文件
    if (![fileManager fileExistsAtPath:filePath]) {
        // 如果没有数据库文件就要从Bundle复制到沙箱
        // 对数据库进行操作时要操作沙箱中的数据库文件
        // 对于只读的数据库也可以直接从Bundle中读取
        NSString *sourceFile = [[NSBundle mainBundle] pathForResource:@"company" ofType:@"db"];
        [fileManager copyItemAtPath:sourceFile toPath:filePath error:nil];
    }
    return YES;
}

说明:这里需要需要导入第三方库 1.导入第三方库FMDB 2.在项目文件 Build Phases, Link Binary With Libraies 添加 libsqlite3.0.dylib 3.导入头文件#import "FMDB.h"

// 从数据库中读取数据
- (void) loadDaModel{
    if (!dataArray) {
        dataArray = [NSMutableArray array];
    }
    // 从嵌入式数据库sqlite3中获取数据
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/company.db"];
    FMDatabase *db = [FMDatabase databaseWithPath:filePath];
    if (db && [db open]) {
        NSString *sql = @"select deptno, dname, dloc, total from tbdept as t1 left outer join (select dno, count(dno) as total from TbEmp group by dno) as t2 on t1.deptno = t2.dno";
        // 执行查询的SQL语句获得结果(游标)
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            // 对查询结果的行进行迭代 从行中取出列
            GPDept *model = [[GPDept alloc] init];
            model.no = [rs intForColumn:@"deptno"];
            model.name = [rs stringForColumn:@"dname"];
            model.location = [rs stringForColumn:@"dloc"];
            model.total = [rs intForColumn:@"total"];
            [dataArray addObject:model];
        }
        [myTableView reloadData];
    }
    else{
        NSLog(@"创建或打开数据库失败");
    }
}
// 向数据库中添加数据
- (IBAction)okButtonClicked:(UIButton *)sender {
    // trim去掉textField输入的空格
    NSString *name = [_nameLabel.text trim];
    NSString *location = [_locationLabel.text trim];
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/temp.db"];
    FMDatabase *db = [FMDatabase databaseWithPath:filePath];
    if (db && [db open]) {
        FMResultSet *rs = [db executeQuery:@"select max(deptno) as maxDno from tbdept"];
        if ([rs next]) {
            int no = [rs intForColumn:@"maxDno"];
            no += 10;
            // 插入数据时通过textField输入的信息传入 这里使用问号
            NSString *sql = @"insert into tbdept values(?,?,?)";
            // 注意:给问号传参数时要传对象 否则会崩在第三方库
            if ([db executeUpdate:sql,@(no),name,location]) {
                GPDept *model = [[GPDepts alloc] init];
                model.no = no;
                model.name = name;
                model.location = location;
                NSNotification *note = [[NSNotification alloc] initWithName:@"addDept" object:model userInfo:nil];
                [[NSNotificationCenter defaultCenter] postNotification:note];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else{
                NSLog(@"添加失败");
            }
        }
    }
}  
上一篇下一篇

猜你喜欢

热点阅读