SQLite 数据库 -- 学习笔记

本文介绍如何使用FMDB库在iOS应用中创建SQLite数据库,并演示了基本的SQL操作,包括创建表、插入数据、查询、更新及删除记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"

//保存  ---->  NSFilehandle
//100 ---->  array    99   ---->  array  100
//数据库 ---->  dic  ---->  哈希表  ----> 哈希算法

//保存数据   ----> 读取制定的数据
//mySql   sql xxx  orcale (10GB)  db2
//sqlite   100kb
//新的语言  ---> SQL语句
//name

//保存数据  最基本的单位  ----> 表 (字段)
//字段类型  int(integer)  float(real)  string(text)  blob(二进制)   bool
//区分唯一数据    主键 (主键不能为空)

//创建数据库
//Student ----> name   age
//创建表
//create table [if not exists] 表名 (字段1 字段类型,字段1 字段类型,字段1 字段类型...);

//create table if not exists Student (id integer primary key autoincrement,name text,age integer);                                   primary |ˈpraɪməri|主要的

//增加数据  insert |ɪnˈsɜːt|插入
//insert into 表名 (字段1,字段2,...) values (值1,值2,....);
//insert into Student (name,age) values ('zhangsan',20);

//查询语句
//查询所有的字段  *
//id  = 1
//name = zhangsan
//select 字段1,字段2,... from 表名 条件(where  (and  or)) order by 字段 asc | desc;
//select name from Student where name = 'zhangsan' or id = 2;
//select * from Student order by age desc;

//修改数据
//update 表名 set 字段名字 = 值 条件(where);
//update Student set name = 'zhangsanfeng' where id = 1;

//删除数据
//delete from 表名 删除所有数据
//delete from 表名 where语句; //<   >   >=   <=

//FMDB  --->  libsqlite3.0.dylib

#import "FMDatabase.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //创建数据库
    //path 沙盒
    NSString *docPath = [NSString stringWithFormat:@"%@/Documents/student.db",NSHomeDirectory()];
    
    FMDatabase *fmDataBase = [FMDatabase databaseWithPath:docPath];
    
    NSLog(@"%@",docPath);
    
    //打开数据库
    [fmDataBase open];
    //所有数据库操作  必须先打开数据
    
    //查询
    //fmDataBase executeQuery:(NSString *), ...
    
    //改变数据内容
    //fmDataBase executeUpdate:(NSString *), ...
    
    NSString *sqlStr = @"create table if not exists Student (id integer primary key autoincrement,name text, age integer)";
    [fmDataBase executeUpdate:sqlStr];
    
    //增加数据  10
    //循环
#if 0
    for(NSInteger i = 0; i < 10;i++)
    {
        NSString *name = [NSString stringWithFormat:@"zhangsan%ld",i];
        NSInteger age = arc4random() % 100 + 1;
        
        sqlStr = @"insert into Student (name,age) values (?,?)";
        
        //fmDataBase 只能增oc对象
        [fmDataBase executeUpdate:sqlStr,name,[NSNumber numberWithInteger:age]];
    }
    
    //修改
    sqlStr = @"update Student set name = ? where id < ?";
    [fmDataBase executeUpdate:sqlStr,@"zhangsanfeng",@(5)];
#endif
    
    //删除
    sqlStr = @"delete from Student";
    [fmDataBase executeUpdate:sqlStr];
    
    //查询
    sqlStr = @"select * from Student";
    FMResultSet *result = [fmDataBase executeQuery:sqlStr];
    
    //判断一个数据 是否存在
    while ([result next]) {
        //类型ForColumn
        NSLog(@"%@",[result stringForColumn:@"name"]);
        NSLog(@"%d",[result intForColumn:@"age"]);
    }
    
    
    [fmDataBase close];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值