又又好久沒見了。想你們。今天寫一篇關於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
沒錯就是這樣,很簡單,大家可以拉到工程中運行下。