FMDB

App.m

UIViewController *vc = [[UIViewController alloc]init];

vc.title = @"FMDB";

UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

self.window.rootViewController = nav;

DataBase.h

+(instancetype)initDataBase;

-(void)initData;

-(void)DataTable;

-(void)addData:(ClassRoom *)thaData;

-(void)deleteData:(NSInteger)theID;

-(void)changeData:(ClassRoom *)theData;

-(NSMutableArray *)DataArray;

-(void)closeData;

DataBase.m

static DataBase *theDataBase;

static FMDatabase *db;

+(instancetype)initDataBase{

if (!theDataBase)

{

theDataBase = [[DataBase alloc]init];

}

return theDataBase;

}

//初始化數(shù)據(jù)庫(kù)

-(void)initData

{

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

NSString *newPath = [path stringByAppendingString:@"/qq.db"];

db = [[FMDatabase alloc]initWithPath:newPath];

if ([db open])

{

NSLog(@"數(shù)據(jù)庫(kù)創(chuàng)建成功");

[self createTable];

}

else

{

NSLog(@"數(shù)據(jù)庫(kù)創(chuàng)建失敗");

}

}

//創(chuàng)建數(shù)據(jù)庫(kù)表+格

-(void)createTable

{

[db executeUpdate:@"create table classroom(intTeger integer primary key,name text,age text)"];

[db close];

}

//添加數(shù)據(jù)

-(void)addData:(ClassRoom *)thaData{

if ([db open])

{

[db executeUpdate:[NSString stringWithFormat:@"insert into classroom values(null,'%@','%@')",thaData.name,thaData.age]];

}

else

{

NSLog(@"添加失敗");

}

[db close];

}

//刪除數(shù)據(jù)

-(void)deleteData:(NSInteger)theId

{

if ([db open])

{

[db executeUpdate:[NSString stringWithFormat:@"delete from classroom where intTeger = '%ld'",theId]];

}

else

{

NSLog(@"刪除失敗");

}

[db close];

}

//修改數(shù)據(jù)

-(void)changerData:(ClassRoom *)theData

{

if ([db open])

{

[db executeUpdate:[NSString stringWithFormat:@"update classroom set name = '%@',age = '%@' where intTeger = '%ld'",theData.name,theData.age,theData.intTeger]];

}

else

{

NSLog(@"需改失敗");

}

[db close];

}

//查詢數(shù)據(jù)

-(NSMutableArray*)dataArray

{

NSMutableArray *arr = [NSMutableArray array];

[db open];

FMResultSet *set = [[FMResultSet alloc]init];

set? = [db executeQuery:@"select *from classroom"];

while ([set next]) {

ClassRoom *room = [[ClassRoom alloc]init];

room.intTeger = [set intForColumn:@"intTeger"];

room.name = [set stringForColumn:@"name"];

room.age = [set stringForColumn:@"age"];

[arr addObject:room];

}

[db close];

return arr;

}

ClassRoom.h

@property(nonatomic,assign)NSInteger integer;

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *age;


ClassView.h

@property(nonatomic,strong)UITextField *nameTf,*ageTf;

ClassView.m

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])

{

[self addSubview:self.nameTf];

[self addSubview:self.ageTf];

}


return self;

}

-(UITextField *)nameTf

{

if (!_nameTf)

{

_nameTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 100, 200, 40)];

_nameTf.borderStyle = UITextBorderStyleRoundedRect;

_nameTf.placeholder = @"please you name";

}

return _nameTf;

}

-(UITextField *)ageTf

{

if (!_ageTf) {

_ageTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 150, 200, 40)];

_ageTf.borderStyle = UITextBorderStyleRoundedRect;

_ageTf.placeholder = @"please you age";

}

return _ageTf;

}


ViewController.m

NSMutableArray *arr;

//添加導(dǎo)航欄

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];

}

-(void)viewWillAppear:(BOOL)animated{

[[DataBase initDataBase]initData];

arr = [[DataBase initDataBase]dataArray];

[self.tableView reloadData];

}

//實(shí)現(xiàn)點(diǎn)擊方法

-(void)click{

SecViewController *add = [[SecViewController alloc]init];

[self.navigationController pushViewController:add animated:YES];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 80;

}

//確定行數(shù)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return arr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@""];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

ClassRoom *classroom = arr[indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@"%ld\n%@\n%@",classroom.intTeger,classroom.name,classroom.age];

cell.textLabel.numberOfLines = 0;

return cell;

}

//刪除數(shù)據(jù)

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

ClassRoom * model = arr[indexPath.row];

[[DataBase initDataBase]deleteData:model.intTeger];

[arr removeObjectAtIndex:indexPath.row];

[self.tableView reloadData];

}

//修改數(shù)據(jù)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

SecViewController * sec = [[SecViewController alloc]init];

sec.room = arr[indexPath.row];

[self.navigationController pushViewController:sec animated:YES];

}

SecViewCon.h

@property(nonatomic,strong)ClassRoom *room;

SecViewCon.m

ClassView *classView;

classView = [[ClassView alloc]initWithFrame:self.view.frame];

classView.backgroundColor = [UIColor whiteColor];

self.view = classView;

classView.nameTf.text = self.room.name;

classView.ageTf.text =? self.room.age;

if (classView.nameTf.text.length<= 0) {

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];

}

else

{

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];

}

}

-(void)save{

ClassRoom *room = [ClassRoom new];

room.name = classView.nameTf.text;

room.age = classView.ageTf.text;

[[DataBase initDataBase]initData];

[[DataBase initDataBase]addData:room];

[self.navigationController popViewControllerAnimated:YES];

}

-(void)edit{

self.room.name = classView.nameTf.text;

self.room.age? = classView.ageTf.text;

[[DataBase initDataBase]initData];

[[DataBase initDataBase]changerData:self.room];

[self.navigationController popViewControllerAnimated:YES];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末坷剧,一起剝皮案震驚了整個(gè)濱河市赌蔑,隨后出現(xiàn)的幾起案子芦岂,更是在濱河造成了極大的恐慌,老刑警劉巖卦溢,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件箫荡,死亡現(xiàn)場(chǎng)離奇詭異特咆,居然都是意外死亡泽腮,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門缩举,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)垦梆,“玉大人匹颤,你說(shuō)我怎么就攤上這事⊥行桑” “怎么了印蓖?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)京腥。 經(jīng)常有香客問(wèn)我另伍,道長(zhǎng),這世上最難降的妖魔是什么绞旅? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮温艇,結(jié)果婚禮上因悲,老公的妹妹穿的比我還像新娘。我一直安慰自己勺爱,他們只是感情好晃琳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著琐鲁,像睡著了一般卫旱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上围段,一...
    開(kāi)封第一講書(shū)人閱讀 51,208評(píng)論 1 299
  • 那天顾翼,我揣著相機(jī)與錄音,去河邊找鬼奈泪。 笑死适贸,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的涝桅。 我是一名探鬼主播拜姿,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼冯遂!你這毒婦竟也來(lái)了蕊肥?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蛤肌,失蹤者是張志新(化名)和其女友劉穎壁却,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體寻定,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡儒洛,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了狼速。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片琅锻。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出恼蓬,到底是詐尸還是另有隱情惊完,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布处硬,位于F島的核電站小槐,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏荷辕。R本人自食惡果不足惜凿跳,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望疮方。 院中可真熱鬧控嗜,春花似錦、人聲如沸骡显。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)惫谤。三九已至壁顶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間溜歪,已是汗流浹背若专。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痹愚,地道東北人富岳。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像拯腮,于是被迫代替她去往敵國(guó)和親窖式。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • #import@interface AppDelegate : UIResponder@property (str...
    本澤馬閱讀 233評(píng)論 0 0
  • 哦吼吼动壤,又研究了幾天萝喘,把FMDB這個(gè)封裝好的數(shù)據(jù)庫(kù)搞定了,寫(xiě)了個(gè)簡(jiǎn)單的例子琼懊,基于FMDB的添刪改查操作阁簸,界面很一般...
    lichengjin閱讀 527評(píng)論 0 0
  • 作者唯一QQ:228544117。哼丈。启妹。。醉旦。 =========后面的都要新建一個(gè)文章 AppDelegate.h ...
    CC_iOS閱讀 850評(píng)論 0 0
  • 用cocoaPods配置第三方文件 第一步饶米。打開(kāi)終端 第二步桨啃。cd+文件夾 第三步。pod init 第四步檬输。打開(kāi)...
    不說(shuō)謊的匹諾曹Y閱讀 1,083評(píng)論 0 1
  • 首先 導(dǎo)入FMDB 并添加FMDB依賴庫(kù)(labslite3.0) 創(chuàng)建model類 如圖 緊接著創(chuàng)建業(yè)務(wù)處理層...
    酷酷的疼閱讀 487評(píng)論 0 0