Ios自己面试总结iOS收藏

FMDB

2016-03-28  本文已影响1172人  小盒盒

FMDB

http://blog.csdn.net/xyz_lmn/article/details/9312837
http://www.jianshu.com/p/82b2b06e3172

1.简介

基本使用

 - 文件路径有三种情况
   - 具体文件路径
   - 如果不存在会自动创建
 - 空字符串@""
   - 会在临时目录创建一个空的数据库
   - 当FMDatabase连接关闭时,数据库文件也被删除

- 执行更新
 - 在FMDB中,除查询以外的所有操作,都称为“更新”
`create、drop、insert、update、delete`等

 - 使用`executeUpdate`:方法执行更新

 ```objc
  -(BOOL)executeUpdate:(NSString*)sql, ...
  -(BOOL)executeUpdateWithFormat:(NSString*)format,       ...
  -(BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments
  示例
  [db executeUpdate:@"UPDATE t_student SET age = ? WHERE name = ?;", @20, @"Jack"]
  ```
 
![Snip20160305_67.png](https://img.haomeiwen.com/i1482869/266515f6bdd1f06d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

- 执行查询

```objc
查询方法
- (FMResultSet *)executeQuery:(NSString*)sql, ...
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
示例
// 查询数据
FMResultSet *rs = [db executeQuery:@"SELECT * FROM t_student"];
// 遍历结果集
while ([rs next]) {
   NSString *name = [rs stringForColumn:@"name"];
   int age = [rs intForColumn:@"age"];
   double score = [rs doubleForColumn:@"score"];
}

FMDatabaseQueue

简单使用
[queue inDatabase:^(FMDatabase *db) {
 [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jack"];
 [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Rose"];
 [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jim"];
 FMResultSet *rs = [db executeQuery:@"select * from t_student"];
 while ([rs next]) {
     // …
 }
}];

使用事务
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
  [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jack"];
  [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Rose"];
  [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jim"];
  FMResultSet *rs = [db executeQuery:@"select * from t_student"];
  while ([rs next]) {
      // …
  }
}];

上一篇 下一篇

猜你喜欢

热点阅读