IOS开发:FMDB数据存储解析
作者:本站整理 时间:2015-11-25
FMDB是面向对象的,它以OC的方式封装了SQLite的C语言API,使用起来更加的方便,不需要过多的关心数据库操作的知识。但是它本身也存在一些问题,比如跨平台,因为它是用oc的语言封装的,所以只能在ios开发的时候使用,如果想实现跨平台的操作,来降低开发的成本和维护的成本,就需要使用比较原始的SQLite。

- // ViewController.m
- // JRFMDB
- //
- // Created by jerehedu on 15/6/18.
- // Copyright (c) 2015年 jerehedu. All rights reserved.
- //
- #import "ViewController.h"
- #import "FMDatabase.h"
- @interface ViewController ()
- @property (nonatomic, strong) FMDatabase *db;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //导入sqlite框架,导入FMDB文件夹
- //1.获得数据库文件的路径
- NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
- NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"];
- NSLog(@"fileName = %@",fileName);
- //2.获得数据库
- FMDatabase *db = [FMDatabase databaseWithPath:fileName];
- //3.打开数据库
- if ([db open]) {
- NSLog(@"ok");
- //4.创表
- BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
- if (result) {
- NSLog(@"创表成功");
- }else{
- NSLog(@"创表失败");
- }
- }
- self.db=db;
- //插入数据
- [self insertStu];
- [self deleteStu:6];
- [self updateStu:@"apple7_name" :@"7777"];
- [self queryStu];
- [self dropStu];
- [self insertStu];
- [self queryStu];
- //6.关闭数据库
- [self.db close];
- }
- #pragma mark 插入数据
- -(void)insertStu
- {
- for (int i=0; i<10; i++)
- {
- NSString *name = [NSString stringWithFormat:@"1apple%i_name",i];
- int age = arc4random()%3+20;
- //1. executeUpdate : 不确定的参数用?来占位 (后面参数必须都是oc对象)
- [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)];
- //2. executeUpdateWithFormat : 不确定的参数用%@、%d等来占位 (参数为原始数据类型)
- // [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age];
- //3. 数组
- // [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]];
- }
- }
- #pragma mark 删除数据
- -(void)deleteStu:(int)idNum
- {
- //a. executeUpdate : 不确定的参数用?来占位 (后面参数必须都是oc对象)
- // [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)];
- //b. executeUpdateWithFormat : 不确定的参数用%@、%d等来占位
- // [self.db executeUpdateWithFormat:@"delete from t_student where name=%@;",@"apple9_name"];
- }
- #pragma mark 销毁表格
- -(void)dropStu
- {
- [self.db executeUpdate:@"drop table if exists t_student;"];
- //4.创表
- BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
- if (result) {
- NSLog(@"再次创表成功");
- }else{
- NSLog(@"再次创表失败");
- }
- }
- #pragma mark 修改数据
- -(void)updateStu:(NSString *)oldName :(NSString*)newName
- {
- // [self.db executeUpdateWithFormat:@"update t_student set name=%@ where name=%@;",newName,oldName];
- [self.db executeUpdate:@"update t_student set name=? where name=?",newName,oldName];
- }
- #pragma mark 查询数据
- -(void)queryStu
- {
- //1.执行查询语句
- // FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"];
- FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)];
- //2.遍历结果集合
- while ([resultSet next]) {
- int idNum = [resultSet intForColumn:@"id"];
- NSString *name = [resultSet objectForColumnName:@"name"];
- int age = [resultSet intForColumn:@"age"];
- NSLog(@"id=%i ,name=%@, age=%i",idNum,name,age);
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
相关文章
相关推荐
-
卓大师刷机专家电脑版 v5.7.0.0
-
易捷录音整理助手官方版 v7.01
-
音悦mini 1.2.16.150529(视频客户端)
-
GeoGebra官方版 v5.0.349.0
-
MyLanViewer 4.19.0(局域网扫描仪)
-
24点速算机免费版 V1.0
-
avast! Pro Antivirus 2015.10.3.2225官方中文专业版(avast杀毒软件)
-
枫叶F4V格式转换器 7.7.0.09(格式转换工具)
-
瑞星之剑安装版 V2.0.0.4
-
华为EMUI5.0系统最新版 v1.0
-
方可出入库管理系统最新版 v9.5
-
wps office 2017个人版 v10.1.0.6207
-
换热器大师(列管式换热器) v2014.0 免费版
-
TXT转PDF工具1.0(文档转换工具)官方下载
-
Autodesk Maya 2015 64位简体中文版
-
YoMail邮箱免费版 v7.8.0.10