iOS中 用FMDB封装一个SQLite数据库详解手机开发
2023-06-13 09:20:07 时间
//写一个修改人的接口
- (void)uodatePerson : (NSString *)age ByPersonID : (NSString *)ID;
//写一个查询所有人的接口
- (NSMutableArray *)selectAllPersonFromPersonTable;
@end
DataBaseHandle.m
#import "DataBaseHandle.h" #import "FMDB.h" #import "PersonModel.h" @implementation DataBaseHandle - (void)dealloc self.db = nil; [super dealloc]; }
创建单例的的接口:
//创建单例对象使其存在于静态区 static DataBaseHandle *handle = nil; //创建单例的的借口 + (DataBaseHandle *)shareDateBaseHandle{ @synchronized(self){ if (handle == nil) { handle = [[DataBaseHandle alloc]init];- (NSString *)dbpath{ return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"db.sqlite"]; }
创建一个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(@"打开失败"); }四种方法:增、删、改、查;
//插入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; }建一个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===============================测试调用===============================
#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]; }
//懒加载 - (NSMutableArray *)dataSource{ if (_dataSource == nil) { self.dataSource = [NSMutableArray arrayWithCapacity:0]; return [[_dataSource retain]autorelease]; }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"]; }配置显示:
#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; }来自:http://blog.csdn.net/qq_31810357/article/details/49181453
3352.html
服务器部署程序员系统优化网站设置运维
相关文章
- iOS编译简析
- iOS 处理pfx文件
- iOS 用UIScrollView不能获取到touchesBegan
- 备份从android到ios系统文件,如何将音乐从安卓设备转移到iOS设备中「建议收藏」
- iOS锁屏时钟_ios时钟怎么调
- AirServer2023从ios无线投屏mac上面教程
- iOS开发遇到的一些小问题记录
- 微信 iOS SQLite 源码优化实践
- iOS获取状态栏和导航栏尺寸(宽度和高度)详解手机开发
- iOS时间类型转换和各种数据类型进行转换详解手机开发
- iOS—-CocoaPods的安装、使用和,原理+参考流程+常见问题详解手机开发
- appLinux开发探索之路:从iOS APP开始(linux开发ios)
- Linux下开发iOS应用:挑战还是机会?(linux做ios开发)
- 用户吐槽iOS 14.6导致iPhone过热和掉电太快
- 迁移升级:从SQLite到MySQL(sqlite转mysql)
- iOS开发与Linux的技术融合(ios开发linux)
- 苹果将在iOS 15中加入扫描相册功能:隐私何在?
- 的区别SQLite和MySQL: 突出的不同特点(sqlite和mysql)
- 探秘iOS:它究竟是一种基于Linux的操作系统吗?(ios是linux系统吗)
- SQL Server连接SQLite实现数据互通(mssql连sqlite)
- 移动端iOS技术实现Oracle数据库连接(ios联接oracle)
- 谁说 iOS 就不适用Oracle(ios oracle软件)
- iOS系统下的Oracle数据库环境(ios oracle)
- 火速围观,国外黑客成功在iPhone 7上越狱iOS 10
- 保护你的Sqlite数据库(SQLite数据库安全秘籍)