1、sqlite(了解即可)-FMDB(必须掌握2个小方法)
第三方库 github上的 FMDB 依赖于sqlite3动态库 libsqlite3.dylib
必须掌握的2个小方法
代码:
// // HMViewController.m #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]; } - (void)query { // 得到结果集 // FMResultSet *set = [self.db executeQuery:@"SELECT * FROM t_shop;"]; // // // 不断往下取数据 // while (set.next) { // // 获得当前所指向的数据 // NSString *name = [set stringForColumn:@"name"]; // double price = [set doubleForColumn:@"price"]; // NSLog(@"%@ %f", name, price); // } } - (void)insert { // for (int i = 0; i<100; i++) { // NSString *name = [NSString stringWithFormat:@"手机-%d", i]; //#warning 这里的字符串不用再加上'' // [self.db executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %d);", name, arc4random()%1000]; // } } @end
// // HMShopTool.h #import <Foundation/Foundation.h> @class HMShop; @interface HMShopTool : NSObject + (NSArray *)shops; + (void)addShop:(HMShop *)shop; @end
// // HMShopTool.m #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
------------------------------
sqlite 的SQL语句注意点:
-
-
not null unique default primary key约束
-
联合主键 用不到 不需要了解
-
sqlite特殊点 primary key === primary key auto_increment不需要写
primary key = not null unique auto_increment
-
主键必须是interger类型
-
A表class_id添加外键为B表的id后 A.class_id的值必须只能设置为 B表的id取值数据
On Delete On Update级联用的很少 不需要掌握
student 表的class_id 引用class表的id表的 如果设置On Delete关系 那么删除 class表的某个id值
那么student.class_id = class.id 的都会被自动删除
----------------------------------
这里不用看
sqlite3函数了解即可 纯c语言的东西 面试不会问
sqlite其他杂项:
plist preference只能设置基本数据
NSCoding可以设置对象
Sqlite3暂时了解即可 等会FMDB是必须掌握的
公司里常用的是Sqlite3 coreData即时通讯时候有