前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS中 用FMDB封装一个SQLite数据库

iOS中 用FMDB封装一个SQLite数据库

原创
作者头像
用户7108768
修改2021-10-29 14:03:27
5310
修改2021-10-29 14:03:27
举报

建立一个单例:

DataBaseHandle.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>
@class PersonModel;
@class FMDatabase;
@interface DataBaseHandle : NSObject
@property(nonatomic,retain)FMDatabase *db;
//创建单例的的接口

(DataBaseHandle *)shareDateBaseHandle;
//创建一个Person表格
(void)creatPersonTable;
//插入person的方法
(void)insertPersonTable : (PersonModel *)person;
//写一个删除人的接口
(void)deletePersonByPerssonID : (NSString *)ID;
//写一个修改人的接口
(void)uodatePerson : (NSString )age ByPersonID : (NSString )ID;
//写一个查询所有人的接口
(NSMutableArray *)selectAllPersonFromPersonTable;
@end</pre> 

 
 DataBaseHandle.m  
   
#import "DataBaseHandle.h"
#import "FMDB.h"
#import "PersonModel.h"
@implementation DataBaseHandle 
(void)dealloc
{
  self.db = nil;
  [super dealloc];
}</pre> 
 
  
 创建单例的的接口:  
//创建单例对象使其存在于静态区
static DataBaseHandle *handle = nil;
//创建单例的的借口 
(DataBaseHandle *)shareDateBaseHandle{
@synchronized(self){

  if (handle == nil) {
      handle = [[DataBaseHandle alloc]init]; 



    }
}
return handle;
 
}</pre>
 写一个私有的方法,返回数据库的路径 
- (NSString *)dbpath{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"db.sqlite"];
}</pre> 


 创建一个Person表格 
//创建一个Person表格

(void)creatPersonTable{
 //初始化数据库对象
  self.db = [FMDatabase databaseWithPath: [self dbpath]];
  //打开数据库
 BOOL isOpen = [self.db open];
  if (isOpen) {

  NSLog(@"打开成功");
  //创建表
 
BOOL isCreat =   [self.db executeUpdate:@"create table if not exists Person(id integer primary key autoincrement,name text,gender text,age integer,salary integer)"];

  NSLog(@"%@",isCreat ? @"创建成功":@"创建失败");

 
}else{

  NSLog(@"打开失败");
 
}
}</pre> 

 四种方法:增、删、改、查; 
   
//插入person的方法
 
(void)insertPersonTable : (PersonModel *)person{
BOOL isInsert =  [self.db executeUpdate:@"insert into Person(name,age)values(?,?)",person.name,person.age];
  NSLog(@"%@",isInsert ? @"插入成功":@"插入失败");

}
//写一个删除的接口

(void)deletePersonByPerssonID : (NSString *)ID{
 BOOL isDelete = [self.db executeUpdate:@"delete from Person where id = ?",ID];
  NSLog(@"%@",isDelete ? @"删除成功":@"删除失败");
}

//写一个修改人的接口

(void)uodatePerson : (NSString )age ByPersonID : (NSString )ID{
 BOOL isUpdate = [self.db executeUpdate:@"update Person set age = ? where id = ?",age,ID];
  NSLog(@"%@",isUpdate ? @"修改成功":@"修改失败");
}

//写一个查询所有人的接口

(NSMutableArray )selectAllPersonFromPersonTable{
  FMResultSet set = [self.db executeQuery:@"select  from Person"];
  NSMutableArray array = [NSMutableArray arrayWithCapacity:0];
  while ([set next]) {
  NSInteger ID = [set intForColumn:@"id"];
  NSString *name = [set stringForColumn:@"name"];
  NSInteger age = [set intForColumn:@"age"];
  //创建Person对象存储信息
  PersonModel *p = [[PersonModel alloc]init];
  p.ID = [NSString stringWithFormat:@"%ld",ID];
  p.name = name;
  p.age = [NSString stringWithFormat:@"%ld",age];
  //添加到数组
  [array addObject:p];
  [p release];
 
  }
  return array;
}</pre> 

 建一个model类 
 
  
   
PersonModel.h
#import <Foundation/Foundation.h>
@interface PersonModel : NSObject
@property(nonatomic,copy)NSString ID;
@property(nonatomic,copy)NSString name;
@property(nonatomic,copy)NSString *age;
@end 

PersonModel.m
import "PersonModel.h"
@implementation PersonModel

(void)dealloc
{
  self.name = nil;
  self.age = nil;
  self.ID = nil;
  [super dealloc];
}
@end</pre> 

 ===============================测试调用===============================

 
#import "FirstViewController.h"
#import "DataBaseHandle.h"
#import "PersonModel.h"
@interface FirstViewController ()
@property(nonatomic,retain)NSMutableArray *dataSource;//接收查询的结果
@end 

@implementation FirstViewController

(void)dealloc
{
  self.dataSource = nil;
  [super dealloc];
}</pre> 

 
//懒加载 
(NSMutableArray *)dataSource{
  if (_dataSource == nil) {

  self.dataSource = [NSMutableArray arrayWithCapacity:0];
 
}
return [[_dataSource retain]autorelease];
}</pre> TEXT:

 
- (void)viewDidLoad {
  [super viewDidLoad];
  //调用并验证
  [[DataBaseHandle shareDateBaseHandle]creatPersonTable];
  NSLog(@"%@",NSHomeDirectory());
  PersonModel *p = [[PersonModel alloc]init];
  p.name = @"小韩哥";
  p.age = @"20";
  //调用插入person的方法
//    [[DataBaseHandle shareDateBaseHandle]insertPersonTable:p];
//接收数据库返回的查询结果
  self.dataSource = [[DataBaseHandle shareDateBaseHandle]selectAllPersonFromPersonTable];
//调用删除人的方法
  [[DataBaseHandle shareDateBaseHandle]deletePersonByPerssonID:@"9"];
 

}</pre> 

 配置显示:


#pragma mark - Table view data source

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
  return 1;
}

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
  return self.dataSource.count;
}



(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath {
  UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:@"firstcell" forIndexPath:indexPath];
  PersonModel *p = self.dataSource[indexPath.row];
  cell.textLabel.text = p.name;
return cell;
}</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档