当前位置:Gxlcms > 数据库问题 > FMDB

FMDB

时间:2021-07-01 10:21:17 帮助过:6人阅读

下载第三方 FMDB

 

业务处理类

#import <Foundation/Foundation.h>

#import "Music.h"

 @interface FMDataBaseCtl : NSObject

 +(instancetype)sharedFMDataBase;

 // 增加

-(void)addNewMusicToDataBase:(Music *)music;

// 删除

-(void)deleteMusicFromDataBase:(Music *)music;

// 获取全部歌手

-(NSMutableArray *)getAllMusic;

 

.m类

#import "FMDataBaseCtl.h"

#import "FMDatabase.h"

#import "Music.h"

static FMDataBaseCtl *_DBCtl = nil;

 @interface FMDataBaseCtl()<NSCopying,NSMutableCopying>

{

    FMDatabase *_fmDB;

    Music * _music;

}

@end

 @implementation FMDataBaseCtl

 #pragma mark - 单例类实现

+(instancetype)sharedFMDataBase

{

    if (_DBCtl == nil) {

        _DBCtl = [[FMDataBaseCtl alloc] init];

        [_DBCtl initDataBase];

    }

    return _DBCtl;

}

+(instancetype)allocWithZone:(struct _NSZone *)zone

{

    if (_DBCtl == nil) {

        _DBCtl = [super allocWithZone:zone];

    }

    return _DBCtl;

}

 -(id)copy

{

    return self;

}

 -(id)mutableCopy

{

    return self;

}

 

-(id)copyWithZone:(NSZone *)zone

{

    return self;

}

 -(id)mutableCopyWithZone:(NSZone *)zone

{

    return self;

}

 #pragma mark - 私有方法

// 初始化数据库、初始化数据表

-(void)initDataBase

{

    // 获得Documents目录路径

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"%@",documentsPath);   

    // 文件路径

    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"musics.sqlite"];

    // 实例化FMDataBase对象

    _fmDB = [FMDatabase databaseWithPath:filePath];

    [_fmDB open];

    // 初始化数据表

    [_fmDB executeUpdate:@"CREATE TABLE musics(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,singer TEXT,album TEXT,url TEXT)"];

    [_fmDB close];

}

 #pragma mark - 接口

// 增加

-(void)addNewMusicToDataBase:(Music *)music

{

    //

    [_fmDB open];   

    //

    [_fmDB executeUpdate:@"INSERT INTO musics(name,singer,album,url) VALUES(?,?,?,?)",music.name,music.singer,music.album,music.url];

     [_fmDB close];

}

 

// 删除

-(void)deleteMusicFromDataBase:(Music *)music

{

    [_fmDB open];

    

    [_fmDB executeUpdate:@"DELETE FROM musics WHERE id = ?",[NSNumber numberWithInteger:music.mID]];

    

    [_fmDB close];

}

 

// 获取全部歌手

-(NSMutableArray *)getAllMusic

{

    [_fmDB open];

    FMResultSet *rs = [_fmDB executeQuery:@"SELECT * FROM musics "];

NSMutableArray *allMusicArr = [NSMutableArray array];

    while ([rs next]) {

        NSInteger Id = [rs intForColumn:@"id"];

        NSString * name = [rs stringForColumn:@"name"];

        NSString * singer = [rs stringForColumn:@"singer"];

        NSString *album = [rs stringForColumn:@"album"];

        NSString * url = [rs stringForColumn:@"url"];

       // NSLog(@"id = %ld, name = %@, singer = %@  url = %@", Id, name, singer, url);

            NSLog(@"name = %@",name);

        Music *m = [[Music alloc]init];

        m.name = name;

        m.singer = singer;

        m.url = url;

        m.album = album;

        m.mID = Id;

        [allMusicArr addObject:m];

    }

    [_fmDB close];

     return allMusicArr;

}

@end

 

music.h

私有类

 

#import <Foundation/Foundation.h>

 

@interface Music : NSObject

@property(nonatomic,assign)NSInteger mID;

@property(nonatomic,strong)NSString *album;

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *url;

@property(nonatomic,strong)NSString *singer;

 

@end

 

music.m(无)

#import "Music.h"

 

@implementation Music

 

@end

 

 

主页面(实例化uitableviewcontroller类).m

#import "MusiclistTableViewController.h"

#import "AddMusicViewController.h"

#import "FMDataBaseCtl.h"

#import "Music.h"

 

@interface MusiclistTableViewController ()

@property(nonatomic,strong)AddMusicViewController *addMusicVC;

@property(nonatomic,strong)NSMutableArray *allTableArr;

 

@end

 

@implementation MusiclistTableViewController

-(NSMutableArray *)allTableArr{

 

    if (!_allTableArr) {

        _allTableArr = [[NSMutableArray alloc]init];

    }

    return _allTableArr;

}

-(AddMusicViewController *)addMusicVC{

    if (!_addMusicVC) {

        _addMusicVC = [[AddMusicViewController alloc]init];

    }

    return _addMusicVC;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"歌曲列表";

    

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

    

}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    _allTableArr = [[FMDataBaseCtl sharedFMDataBase]getAllMusic];

    

    

    NSLog(@"---------%@---------",self.allTableArr);

 

    //刷新表格

    [self.tableView reloadData];

    

}

 

-(void)ItemAction:(UIBarButtonItem *)sender{

    

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

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark - Table view data source

 

 

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

    return _allTableArr.count;

    

}

 

 

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

    NSString *identifier = @"cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    

    

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    }

    

    Music *m = _allTableArr[indexPath.row];

    

    cell.textLabel.text = m.name;

   // cell.detailTextLabel.textColor = [UIColor brownColor];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"歌手:%@,《%@》,<%@>",m.singer,m.album,m.url];

    NSLog(@"-------%@",m.name);

    

    

    return cell;

}

 

 

 

 //Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

   //  Return NO if you do not want the specified item to be editable.

    return YES;

}

 

 

 

// Override to support editing the table view.

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source

        

        Music *m = _allTableArr[indexPath.row];

        [[FMDataBaseCtl sharedFMDataBase]deleteMusicFromDataBase:m];

        [_allTableArr removeObjectAtIndex:indexPath.row];

        //[self.allTableArr removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

 

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

    }   

}

 

 

添加数据页面(实例化一个uiviewcontroller).h

 

@property (strong, nonatomic) IBOutlet UITextField *MusicNameTF;

@property (strong, nonatomic) IBOutlet UITextField *singerTF;

@property (strong, nonatomic) IBOutlet UITextField *albumTF;

@property (strong, nonatomic) IBOutlet UITextField *MusicURLTF;

- (IBAction)addMusicHandleBtn:(id)sender;

 

    .m页面

#import "AddMusicViewController.h"

#import "FMDataBaseCtl.h"

#import "Music.h"

 

@interface AddMusicViewController ()

 

@end

 

@implementation AddMusicViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

     self.navigationItem.title = @"添加歌曲";

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

- (IBAction)addMusicHandleBtn:(id)sender {

    NSString *name = self.MusicNameTF.text;

    NSString *singer = self.singerTF.text;

    NSString *album = self.albumTF.text;

    NSString *url  =self.MusicURLTF.text;

    

    Music *m = [[Music alloc]init];

    m.name = name;

    m.singer  =singer;

    m.album = album;

    m.url  = url;

    

    [[FMDataBaseCtl sharedFMDataBase]addNewMusicToDataBase:m];

    

    [self.navigationController popToRootViewControllerAnimated:YES];

    

}

@end

 

FMDB

标签:

人气教程排行