图书查询

#import "TableViewController.h"

#import "InsertViewController.h"

#import "DataHandel.h"

#import "book.h"

#import "MJRefresh.h"

@interface  TableViewController ()

{

    NSMutableArray *tableArr;

}

@end


@implementation TableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"图书列表";

    //导航栏右按钮                 t t d s s s d s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s d d d

    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(addBook)];

    

    self.navigationItem.rightBarButtonItem = right;

    [self setUpRefresh];


}

-(void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    tableArr = [[DataHandel sharedFMDB]selectAll];

    NSLog(@"%@",tableArr);

    [self.tableView reloadData];

}

-(void)setUpRefresh

{

    [self.tableView addHeaderWithTarget:self action:@selector(headerRefreshBook) dateKey:@"table"];

    self.tableView.headerPullToRefreshText = @"下拉刷新";

}

-(void)headerRefreshBook

{

    book *b = [[book alloc]init];

    b.name = @"张超的操蛋人生";

    b.author = @"熊哥";

    b.price = @"100";

    [[DataHandel sharedFMDB]insertBook:b];

    [tableArr insertObject:b atIndex:0];

    [self.tableView reloadData];

    [self.tableView headerEndRefreshing];

}

-(void)addBook

{

    InsertViewController *insert = [[InsertViewController alloc]init];

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

}




#pragma mark - Table view data source




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

    return tableArr.count;

}


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

{

    static NSString *str = @"sd";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

    if (!cell) {

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

    }

    book *b = [tableArr objectAtIndex:indexPath.row];

    cell.textLabel.text = b.name;

    cell.detailTextLabel.text = [NSString stringWithFormat:@"作者:%@  价格:%@",b.author,b.price];

    return cell;

}


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

{

    book *b = [tableArr objectAtIndex:indexPath.row];

    [[DataHandel sharedFMDB]deleteBook:b];

    [tableArr removeObjectAtIndex:indexPath.row];

    [self.tableView reloadData];

}


@end












#import <UIKit/UIKit.h>


@interface InsertViewController : UIViewController

@property (strongnonatomicIBOutlet UITextField *name;

@property (strongnonatomicIBOutlet UITextField *author;

@property (strongnonatomicIBOutlet UITextField *price;






- (IBAction)save:(id)sender;


@end







#import "AppDelegate.h"

#import "TableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TableViewController *tab = [[TableViewController allocinitWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationController allocinitWithRootViewController:tab];

    self.window.rootViewController  = nav;

    return YES;

}

#import "AppDelegate.h"

#import "TableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TableViewController *tab = [[TableViewController allocinitWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationController allocinitWithRootViewController:tab];

    self.window.rootViewController  = nav;

    return YES;

}

#import "AppDelegate.h"

#import "TableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TableViewController *tab = [[TableViewController allocinitWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationController allocinitWithRootViewController:tab];

    self.window.rootViewController  = nav;

    return YES;

}

#import "AppDelegate.h"

#import "TableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TableViewController *tab = [[TableViewController allocinitWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationController allocinitWithRootViewController:tab];

    self.window.rootViewController  = nav;

    return YES;

}

#import "AppDelegate.h"

#import "TableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TableViewController *tab = [[TableViewController allocinitWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationController allocinitWithRootViewController:tab];

    self.window.rootViewController  = nav;

    return YES;

}




#import "InsertViewController.h"

#import "DataHandel.h"

#import "book.h"


@interface InsertViewController ()


@end


@implementation InsertViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

}




/**

 *  添加方法

 *

 */

- (IBAction)save:(id)sender

{

    book *b = [[book allocinit];

    b.name = self.name.text;

    b.author = self.author.text;

    b.price = self.author.text;

    [[DataHandel sharedFMDB]insertBook:b];

    [self.navigationController popToRootViewControllerAnimated:YES];

    [[[UIAlertView alloc]initWithTitle:@"添加成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitlesnil]show];

    

}

@end












#import <Foundation/Foundation.h>

#import "FMDatabase.h"

#import "book.h"


@interface DataHandel : NSObject

+(instancetype)sharedFMDB;


-(void)insertBook:(book *)bo;

-(void)deleteBook:(book *)bo;

-(NSMutableArray *)selectAll;

@end












#import "DataHandel.h"


static DataHandel *data = nil;

static FMDatabase *fmdb = nil;

@implementation DataHandel

/**

 *  单例

 */

+(instancetype)sharedFMDB

{

    if (data == nil) {

        data = [[DataHandel allocinit];

        [data initFMDB];

    }

    return data;

}

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

{

    if (!data) {

        data = [super allocWithZone:zone];

    }

    return data;

}

-(id)mutableCopy

{

    return self;

}

-(id)copy

{

    return self;

}


//创建数据库

-(void)initFMDB

{

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES)lastObject];

    //*********************************** 换成自己名字**************************//

    NSString *path = [docPath stringByAppendingPathComponent:@"zhouwenhuan.sqlite"];

    

    NSLog(@"%@",path);

    

    fmdb = [FMDatabase databaseWithPath:path];

    if ([fmdb open]) {

        [fmdb executeUpdate:@"CREATE TABLE book(idd INTEGER PRIMARY KEY AUTOINCREMENT , name TEXT, author TEXT, price TEXT)"];

        [fmdb close];

        

    }

    else

        NSLog(@"创建数据表失败");

}

/**

 *  

 *

 *  @return 所查出来的数组返回

 */

-(NSMutableArray *)selectAll

{

    NSMutableArray *arr = [NSMutableArray array];

    [fmdb open];

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

    fmSet = [fmdb executeQuery:@"SELECT * FROM book"];

    while ([fmSet next]) {

        int idd = [fmSet intForColumn:@"idd"];

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

        NSString *author = [fmSet stringForColumn:@"author"];

        NSString *price = [fmSet stringForColumn:@"price"];

        

        book *b = [[book alloc]init];

        b.idd = idd;

        b.name = name;

        b.author = author;

        b.price = price;

        NSLog(@"id = %d",idd);

        [arr addObject:b];

    }

    [fmdb close];

    return arr;

}

/**

 *  

 *

 *  @param book Model

 */

-(void)insertBook:(book *)bo

{

    BOOL flag = false;

    NSArray *arrBook = [self selectAll];

    for (book *b in arrBook) {

        if ([b.name isEqualToString:bo.name ] && [b.author isEqualToString:bo.author] && [b.price isEqualToString:bo.price]) {

            flag = true;

        }

    }

    if (flag) {

        NSLog(@"数据相同");

    }

    else

    {

        [fmdb open];

        BOOL addFlag = [fmdb executeUpdate:@"INSERT INTO book VALUES(null,?,?,?)",bo.name,bo.author,bo.price ];

        if (addFlag) {

            NSLog(@"添加成功");

        }

        else

        {

            NSLog(@"添加失败");

        }

        [fmdb close];

    }


}

/**

 *  

 *

 *  @param book Model

 */

-(void)deleteBook:(book *)bo

{

    [fmdb open];

    NSString *sql = [NSString stringWithFormat:@"DELETE FROM book WHERE idd = %d",bo.idd];

    BOOL deleteFlag = [fmdb executeUpdate:sql];

    if (deleteFlag) {

        NSLog(@"删除成功");

    }

    else

        NSLog(@"删除失败");

    [fmdb close];

}


@end











#import <Foundation/Foundation.h>


@interface book : NSObject

@property (nonatomic,strong)    NSString *name;

@property (nonatomic,strong)    NSString *author;

@property (nonatomic,strong)    NSString *price;

@property (nonatomic,assign)    int idd;

@end










#import "AppDelegate.h"

#import "TableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TableViewController *tab = [[TableViewController allocinitWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationController allocinitWithRootViewController:tab];

    self.window.rootViewController  = nav;

    return YES;

}


你可能感兴趣的:(图书查询)