FMDB的使用

使用FMDB框架创建本地数据库存储数据:
使用步骤入下:

/** 1> 创建数据库的存储路径 */
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)  lastObject];
NSString *dbPath = [docPath stringByAppendpathCompoent:@"user.sqlite"];

/** 2> 根据存储路径创建数据库 */
FMDatabase *fmdb = [FMDatabase databaseWithPath:docPath];

/** 3> 打开数据库 并创建表 然后关闭数据库 */
[fmdb open];

    if ([fmdb open]) {

        NSLog(@"数据库打开成功");
    }

    /** 创建表 */
    BOOL success =[fmdb executeUpdate:@"CREATE TABLE ZJSJ (ID INTEGER PRIMARY KEY AUTOINCREMENT,zjname text,zjimage text)"];

    if (success) {
        NSLog(@"表创建成功");
    }
    [fmdb close];

iOS 中的数据的存储:
plist (NSArray,NSDictionary)
Preference(NSUserDefaults)
NSCoding (任何对象)
sqlite3 数据库 纯C语言 轻量级
Core Data 数据库 基于sqlite3 OC版本 重量级

sqlite是一款轻型的嵌入式数据库(广泛的应用于移动端)
占用资源非常低
处理速度快

数据库如何存储数据的

以表的形式存储数据 表中的列 称为字段
表中的行 称为记录
数据库存储数据的步骤
1>新建表
2>添加字段
3>添加记录

navicat 的使用:
1> 创建一个连接connection;此链接是要连接到指定数据库的(相当于数据库文件),数据库的存储路径
2> 此时数据库(main)默认已经创建好了
3> 在数据库中创建表(table)
4> 添加各种不同数据类型的字段;
5> 命名表名称 t_shop (t下划线开头)
6> 添加记录数据

sqlite数据库 实际上是没有类型的 划分的类型 是为了一种规范

SQL 结构化查询语言:是一种对关系型数据库中的数据进行定义和操作的语言。

数据定义语句DDL:
1> 创表 : create table 表名(字段1 类型,字段2 类型 …..);
* create table if not exist t_shop(name text , price real); *
2> 删表
* drop table if exists t_shop; *

数据操作语句DML:
1> 增:
insert into t_shop (name,price,left_count) values (‘手机’,1500,50);
2> 改:
update t_shop set price = 900,left_count = 200;
3> 删:
delete from t_shop;

数据查询语句DQL
select name,price,left_count from t_shop;
select * from t_shop;
select count(name) from t_shop; 计算记录数量
select count(name) from t_shop where left_count > 100;
select count(*) from t_shop where left_count > 100;
select count(*) 剩余数量 from t_shop where left_count > 100;
select from t_shop where left_count > 100;
select * from t_shop order by left_count //排序 默认升序
select * from t_shop order by left_count desc; 降序
select * from t_shop order by left_count desc ,price asc;

使用limit可以精确的控制查询结果的数量,比如每次值查询10条数据;
select * from t_shop limit 4,10; 从第4条开始,查询后面的8条数据.
select * from t_shop order by price asc limit 0,10;
limit 常用来做分页查询,
例如:
第一页 : limit 0,5;
第一页 : limit 5,5;
第一页 : limit 10,5;
limit 7 等价于 limit 0,7;

别名的使用:
select name shop_name ,price AS shop_price ,left_count AS 库存 t_shop;

条件语句:
update t_shop set left_count = 0 while price < 1000;
delete from t_shop where left_count = 0;
delete from t_shop where left_count < 500 and price < 2000;
delete from t_shop where left_count < 500 or price < 2000;

简单的约束:(约束都是在创表的时候添加的) 保证数据的规范性
1> 规定某些字段值不能为空:
create table if not exists t_student (name text not null,age int not null);
2> 规定某些字段值不能出现重复 即唯一
create table if not exists t_student (name text not null unique, age int not null);
3> 规定某个字段的默认值
create table if not exists t_student (name text not null unique,age int default 20);

主键约束
1. 用来唯一的标识某一条记录;
create table if not exists t_student (id integer primary key,name text ,age integer);
2. 要想主键能自动增长,必须要设置成integer类型

外键约束
1. 利用外键约束可以用来建立表与表之间的联系
2. 一张表的某个字段引用着另一张表的主键。外键一般被引用数据量多的那个表中。
3. 嵌套查询: 创建两张表 菜系(8种) 菜(800种)
create table if not exists t_food_type(id integer primary key auto increment ,name text not null);
create table if not exists t_food (id integer primary key autoincrement, name text not null, food_type_id integer, constraint fk_food_foodtype foreign key [food_type_id] references t_food_type [id]);

问题: 查询所有的粤菜:

select * from t_food where food_type_id = (select id from t_food_type where name = @”粤菜”);
select * from t_food f, t_food_type ft where f.id = ft.food_type_id and ft.name = @”粤菜”;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值