一、下载fmdb类库

二、添加libsqulite3.0.dylib

三、添加头文件#import "FMDB.h"

四、打开数据库

a、设置路径NSString *path = [[NSSearchPathForDirectoriesInDomainas(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]  stringByAppendingPathComponent:@"shops.sqlite"];

b、设置强引用属性 @property (nonatomic, strong) FMDatabase *db;

c、self.db =  [FMDatabase databaseWithPath:path];//返回一个数据库对象

d、打开 [self.db open];

//两个重要语句

//executeQuery:查询数据

//executeUpdate:除查询数据以外的其他操作

五、创表

[self.db execureUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (if integer PRIMARY KEY, name text NOT NULL, price real);"];

六、插入数据

NSString *name = [NSString stringWithFormat:@"手机-%d",i];

[self.db executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %d);",name,arc4random()%1000];

七、读取数据

a、得到结果集

FMResultSet *set = [self.db executeQuery:@"SELECT * FROM t_shop;"];

b、不断取出数据

while(set.next){

//获得当前所指向的数据

NSString *name = [set stringForColumn:@"name"];

double price =[set doubleForColumn:@"price"];

NSLog(@"%@  %f",name , price);

}

八、删除数据

[self.db executeUpdate:@"DELETE FROM t_SHOP WHERE price < 500;"];

九、封装数据库

a、不让控制器接触数据库,设置一个工具类ShopTool

b、设置数据模型Shop

c、在ShopTool中,将db设置成全局变量

static FMDatabase *_db;

b、初始化 +(void)initalize

.......

封装的ShopTool代码:

#import <Foundation/Foundation.h>
@class HMShop; @interface HMShopTool : NSObject
+ (NSArray *)shops;
+ (void)addShop:(HMShop *)shop;
@end #import "HMShopTool.h"
#import "FMDB.h"
#import "HMShop.h" @implementation HMShopTool static FMDatabase *_db; + (void)initialize
{
// 1.打开数据库
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shops.sqlite"];
_db = [FMDatabase databaseWithPath:path];
[_db open]; // 2.创表
[_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, name text NOT NULL, price real);"];
} + (void)addShop:(HMShop *)shop
{
[_db executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %f);", shop.name, shop.price];
} + (NSArray *)shops
{// 得到结果集
FMResultSet *set = [_db executeQuery:@"SELECT * FROM t_shop;"]; // 不断往下取数据
NSMutableArray *shops = [NSMutableArray array];
while (set.next) {
// 获得当前所指向的数据
HMShop *shop = [[HMShop alloc] init];
shop.name = [set stringForColumn:@"name"];
shop.price = [set doubleForColumn:@"price"];
[shops addObject:shop];
}
return shops;
}
@end

控制器代码:

#import "HMViewController.h"
//#import "FMDB.h"
#import "HMShop.h"
#import "HMShopTool.h" @interface HMViewController ()
//@property (nonatomic, strong) FMDatabase *db;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 1.打开数据库
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shops.sqlite"];
// self.db = [FMDatabase databaseWithPath:path];
// [self.db open];
//
// // 2.创表
// [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, name text NOT NULL, price real);"];
// executeQuery:查询数据
// [self.db executeQuery:<#(NSString *), ...#>]; // executeUpdate:除查询数据以外的其他操作
// [self.db executeUpdate:<#(NSString *), ...#>];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// for (int i = 0; i<100; i++) {
// HMShop *shop = [[HMShop alloc] init];
// shop.name = [NSString stringWithFormat:@"枕头--%d", i];
// shop.price = arc4random() % 200;
// [HMShopTool addShop:shop];
// } NSArray *shops = [HMShopTool shops];
for (HMShop *shop in shops) {
NSLog(@"%@ %f", shop.name, shop.price);
} // [self.db executeUpdate:@"DELETE FROM t_shop WHERE price < 800;"];
//
// [self query];
}

shop.h模型代码:

#import <Foundation/Foundation.h>

@interface HMShop : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) double price;
@end
 
 

ios数据库FMDB的更多相关文章

  1. IOS数据库FMDB增、删、改、查的使用【原创】

    http://blog.it985.com/13588.html IOS数据库FMDB增.删.改.查的使用[原创] FMDB是一个XCODE的中一个轻量级的数据库,用于将网络资源存储在本地.所以,FM ...

  2. iOS本地存储-数据库(FMDB)

    初识FMDB iOS中原声的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较麻烦,于是就出现了一系列将SQLite封装的库.本文讲解的FMDB就是其中的一个. FMDB PK ...

  3. ios开发FMDB导入SQLCipher加密数据库

    转:http://www.2cto.com/kf/201407/315727.html [iOS]FMDB/SQLCipher数据库加解密,迁移

  4. iOS 数据库操作(使用FMDB)

    iOS 数据库操作(使用FMDB)   iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...

  5. [iOS]数据库第三方框架FMDB详细讲解

    [iOS]数据库第三方框架FMDB详细讲解 初识FMDB iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较麻烦.于是,就出现了一系列将SQLite API进行封 ...

  6. iOS开发 数据库FMDB

    iOS开发  数据库FMDB 1.简介 需求作用: 如果需要保存大量的结构较为复杂的数据时候, 使用数据库, 例如交规考试项目 常用的数据库: (1)Microsoft SQL Server 2000 ...

  7. iOS——使用FMDB进行数据库操作(转载)

    iOS 使用FMDB进行数据库操作 https://github.com/ccgus/fmdb [摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考. FMDB 使用方法 A ...

  8. IOS数据库操作SQLite3使用详解(转)

    iPhone中支持通过sqlite3来访问iPhone本地的数据库.具体使用方法如下1:添加开发包libsqlite3.0.dylib首先是设置项目文件,在项目中添加iPhone版的sqlite3的数 ...

  9. iOS数据库操作之coredata详细操作步骤

    CHENYILONG Blog iOS数据库操作之coredata详细操作步骤 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/ ...

随机推荐

  1. 关于UIImage类的对象两种初始化方法的区别

    1.imageNamed: UIImage *image = [UIImage imageNamed:"]; UIImage的类方法 第一次读取图片的时候,先把这个图片放到缓存中,下次再使用 ...

  2. Latex 中宽度的设置和理解

    \textwidth, 文本区域的全部宽度 \columnwidth, 文本中一列的宽度,单栏或者多栏的情况下,值是不同的 但是,一旦\textwidth, \columnwidth, \linewi ...

  3. 【新闻】超灵敏MRI技术:照亮人体肺部

    人口健康直接影响到一个国家的经济发展和社会进步.据我国2013年发布的肿瘤发病率统计年报表明,肺癌是我国目前首位恶性肿瘤,是癌症死亡的头号杀手,目前城市中每4名死亡的癌症患者中,约有1名是肺癌.如何开 ...

  4. 手把手教你入门mac idea

    一.前沿 去年入职后, 公司有很多人使用的是idea , 而不是eclipse. 之前就想转向idea, 但一直没转过来~~原因是团队的人提倡用开源eclipse.现在下定决心转向idea. 虽然, ...

  5. tomcat 虚拟目录与显示目录中文件列表

    虚拟目录: 该方法推荐使用,比较简单. 在%tomcat%\conf\Catalina\localhost(该目录可能需要手工创建)下新建一个文件abc.xml,注意文件名中的abc就表示虚拟目录的名 ...

  6. 【转】于request.getSession(true/false/null)的区别

    http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原 ...

  7. SPOJ - OTOCI LCT

    OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...

  8. BZOJ 1927: [Sdoi2010]星际竞速 费用流

    1927: [Sdoi2010]星际竞速 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  9. main()函数的输入参数 main(int argc, char** argv)

    一般简单的C++程序,main函数的写法都是 int main() {... ; return 0;},但是,如果在运行程序时需要有参数输入,可以是使用将主函数写成int main(int argv, ...

  10. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...