ios FMDB初体验之增删改查
又又好久没见了。想你们。今天写一篇关于fmdb的使用文章。在工作中,我们有时会用到数据库,所以你会需要一个好用的工具。长话短说开始进入正题。
先导入fmdb库文件。还有就是加入libsqlite3.dylib。这2个前期准备好后,我们就可以开工了。
#import "ViewController.h" #import "FMDB.h" //导入头文件 @interface ViewController () @property(nonatomic,retain) FMDatabaseQueue *fmQueue;//添加一个队列属性 @end @implementation ViewController //你数据库的路径 -(FMDatabaseQueue *)fmQueue { if (_fmQueue== nil) { NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/student.sqlite"]; _fmQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath]; NSLog(@"%@",dbPath); } return _fmQueue; } - (void)viewDidLoad { [super viewDidLoad]; [self createAllBtn]; //这是在一个代码块中 [self.fmQueue inDatabase:^(FMDatabase *db) { //创建一个表 [db executeUpdate:@"create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer)"]; //保持数据库开启 if (![db open]) { return ; } }]; } -(void)createAllBtn { UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(100, 50, 100, 50); [btn setTitle:@"增加数据" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem]; btn1.frame = CGRectMake(100, 100, 100, 50); [btn1 setTitle:@"删除数据" forState:UIControlStateNormal]; [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn1 addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem]; btn2.frame = CGRectMake(100, 150, 100, 50); [btn2 setTitle:@"修改数据" forState:UIControlStateNormal]; [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(modifyAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeSystem]; btn3.frame = CGRectMake(100, 200, 100, 50); [btn3 setTitle:@"查询数据" forState:UIControlStateNormal]; [btn3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn3 addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn3]; } -(void)increaseAction:(UIButton *)btn { NSLog(@"增加数据"); [self.fmQueue inDatabase:^(FMDatabase *db) { for (int i = 0; i<100; i++) { NSString *name = [NSString stringWithFormat:@"james"]; int age = 10; [db executeUpdate:@"insert into t_student (name,age) values (?,?)",name,[NSNumber numberWithInt:age]]; } }]; } -(void)deleteAction:(UIButton *)btn { NSLog(@"删除数据"); [self.fmQueue inDatabase:^(FMDatabase *db) { NSString *name = [NSString stringWithFormat:@"james"]; //注意下这样是不能删除指定数据的,一定要是字符串nsstring类型 //[db executeUpdate:@"delete from t_student where name = '%@'",name]; //也就是这样 NSString *sql = [NSString stringWithFormat:@"delete from t_student where name = '%@'",name]; [db executeUpdate:sql]; }]; } -(void)modifyAction:(UIButton *)btn { NSLog(@"修改数据"); [self.fmQueue inDatabase:^(FMDatabase *db) { NSString *name = [NSString stringWithFormat:@"wq"]; NSString *set = [NSString stringWithFormat:@"UPDATE t_student SET name = '%@'",name]; [db executeUpdate:set]; }]; } -(void)searchAction:(UIButton *)btn { NSLog(@"查询数据"); [self.fmQueue inDatabase:^(FMDatabase *db) { NSString *sql = [NSString stringWithFormat:@"SELECT *from t_student"]; FMResultSet *set = [db executeQuery:sql]; while ([set next]) { int stuId = [set intForColumn:@"id"]; NSString *name = [set stringForColumn:@"name"]; int age = [set intForColumn:@"age"]; NSLog(@"stuId = %d, name = %@, age = %d", stuId, name, age); //这里的textview用于显示查询到的数据 UITextView *text = [[UITextView alloc]init]; text.frame = CGRectMake(50, 300, 300, 200); text.backgroundColor = [UIColor grayColor]; [text setText:name]; [self.view addSubview:text]; } }]; } @end
没错就是这样,很简单,大家可以拉到工程中运行下。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。