
下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
#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
#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]; } } return handle;} - (Nsstring *)dbpath{ return [[NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES)lastObject] stringByAppendingPathComponent:@"db.sqlite"]; } //创建一个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,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;@endPersonModel.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
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS中 用FMDB封装一个SQLite数据库全部内容,希望文章能够帮你解决iOS中 用FMDB封装一个SQLite数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)