SQL数据库SQLite

来源:互联网 发布:手机进入网络管理网址 编辑:程序博客网 时间:2024/06/03 18:55

SQL常用语句

//数组

//写文件 data string
//NSU
//NSF
//100000000 —> 99

//数据库 写文件 —> dict key value –> 哈希算法

//基本单位 表(字段 字段的类型)

//出现一个区分唯一的数据字段 主键

//二进制位
//整形 integer
//浮点型 double real
//字符串 varchar() —> text
//bool bool boolean
//二进制数据 blob

//操作数据库 —> SQL语句

//Student name age
//创建一个表
//create table if not exists 表名 (字段名字1 字段类型 primary key,字段名字2 字段类型 not null,….);

//create table if not exists Student (name text,age integer);

//插入数据
//insert into 表名 (字段1,字段2,…) values (值1,值2,…);

//
//insert into Student (name,age) values (‘zhangsan’,10);

//查询数据
//查询所有的字段的值 *
//select 字段名1,字段2,… from 表名 where 条件 order by 字段 desc | asc;多个条件 可以用 and or

//select name from Student;
//select * from Student where name = ‘zhangsan’;
//select * from Student order by age asc;

//修改
//update 表名 set 字段名 = 值 where 条件;
//update Student set age = 1000 where name = ‘zhangsan’;

//删除
//delete from 表名 where 条件; 不带条件删除所有的数据
//delete from Student where name = ‘zhangsan’;

//SQLite

import “FMDatabase.h”

//1.libsqlite3.0.dylib

import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *docPath = [NSString stringWithFormat:@”%@/Documents/stu.db”,NSHomeDirectory()];
    NSLog(@”%@”,docPath);
    //1.必须先创建一个数据库
    FMDatabase *dataBase = [FMDatabase databaseWithPath:docPath];

    //打开文件
    [dataBase open];

    //2 创建表
    //id 设定为主键 并且值自动增长
    NSString *sqlStr = @”create table if not exists Student (id integer primary key autoincrement,name text,age integer)”;

    [dataBase executeUpdate:sqlStr];

if 0
//插入数据
//? –> %ld sql语句中的占位符
sqlStr = @”insert into Student (name,age) values (?,?)”;

for (NSInteger i = 0; i < 10; i++) {    NSString *name = [NSString stringWithFormat:@"qianfeng%ld",i];    NSInteger age = arc4random() % 100 + 1;    //executeUpdate 参数 必须都是对象 不能是基本数据类型    [dataBase executeUpdate:sqlStr,name,[NSNumber numberWithInteger:age]];}

endif

//查询数据sqlStr = @"select * from Student where age < 50";FMResultSet *set = [dataBase executeQuery:sqlStr];while ([set next]) {    NSLog(@"%@",[set stringForColumn:@"name"]);    NSLog(@"%d",[set intForColumn:@"age"]);}//更新数据sqlStr = @"update Student set age = ? where name = ?";[dataBase executeUpdate:sqlStr,@100,@"qianfeng0"];//删除sqlStr = @"delete from Student";[dataBase executeUpdate:sqlStr];//关闭文件[dataBase close];//封装//登录   --->  用户和密码   查询//注册   --->  先判断 用户 是否存在(查询数据库)  不存在  插入数据库//修改密码 --->  用户

}

0 0
原创粉丝点击