ios运用fmdb建SQLite数据库

来源:互联网 发布:js 定义json对象 编辑:程序博客网 时间:2024/04/30 04:44

YcwViewController.h文件

@interface YcwViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *myTable;

@end
YcwViewController.m文件

@interface YcwViewController ()
{   int i;
    NSMutableArray *array_jobs;
}
@end

@implementation YcwViewController
-  (void)viewWillAppear:(BOOL)animated     //当第二个页面返回第一个页面时重新加载tableView的数据源,从而更新数据
{
    
    NSLog(@"hello");
    [self.myTable reloadData];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    array_jobs = [NSMutableArray array];
    
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self createDB];
    
    [self loadJobs];
    i=2;
    
    self.title = @"职位信息";
    
    
    
}




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

    return [array_jobs count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellIden = @"myCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIden];
    }
    
    Job *job = [array_jobs objectAtIndex:indexPath.row];
    cell.textLabel.text = job.jobName;
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d  %@",job.jobMoney,job.jobComment];
    
    return cell;
    
}


- (void)createDB       //创建数据库
{
    NSString *path = [self getDBPath];
    
    NSLog(@"%@",path);
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:path]) {
        //若数据库不存在,则创建一个新的数据库
        FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
        
        if ([db open]) {
            
            NSString *sql = @"CREATE TABLE 'position' ('id' INTEGER ,'jobName' VARCHAR ,'jobMoney' INTEGER,'jobComment' VARCHAR)";
            
            BOOL res = [db executeUpdate:sql];
            if (res) {
                NSLog(@"创建成功");
                [db close];
            }
        }
        
        
    }
    
    
    

}

- (NSString *)getDBPath     //得到数据库文件的路径
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    return  [NSString stringWithFormat:@"%@/db5.sqlite",documentsDirectory];
    
}

- (void)loadJobs      //加载数据库文件的数据
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
    if (db.open) {
        NSString *sql = @"select * from position";
        FMResultSet *set = [db executeQuery:sql];
        
        while ([set next]) {             //一行一行的读取数据库文件的数据
            int jobId = [set intForColumn:@"id"];
            
            NSString *jobName = [set stringForColumn:@"jobName"];
            int jobMoney = [set intForColumn:@"jobMoney"];
            NSString *jobComment = [set stringForColumn:@"jobComment"];
            
            Job *job= [[Job alloc]init];
            
            job.id = jobId;
            job.jobName = jobName;
            job.jobMoney = jobMoney;
            job.jobComment = jobComment;
            [array_jobs addObject:job];
            
        }
        [db close];
        
    }
    
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString: @"segue101"]) {
        
        YcwAddViewController *view = (YcwAddViewController *)segue.destinationViewController;
        view.array = array_jobs;        //把第一个页面的tableView的数据源传给第二个页面,可以在第二个页面添加数据
        
    }

}


@end

YcwAddViewController.h文件

@interface YcwAddViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *jobNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *jobMoneyTextField;
@property (strong, nonatomic) IBOutlet UITextField *jobCommentTextField;
- (IBAction)addPosition:(id)sender;
@property (strong,nonatomic) NSMutableArray *array;
@end


YcwAddViewController.m文件

- (IBAction)addPosition:(id)sender {
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
    
    NSString *jobname = self.jobNameTextField.text;
    int jobmoney = [self.jobMoneyTextField.text intValue];
    NSString *jobcomment = self.jobCommentTextField.text;
    Job *job = [[Job alloc]init];
    job.jobName = jobname;
    job.jobMoney = jobmoney;
    job.jobComment = jobcomment;
    [self.array addObject:job];      //向第一个页面的数据源添加元素
    
    NSLog(@"%@",[self getDBPath]);
    if ([db open]) {
        NSLog(@"数据库开启成功");
        NSString *sql = [NSString stringWithFormat:@"insert into position values(001,'%@',%d,'%@')",jobname,jobmoney,jobcomment];    //sql语句,向数据库中添加数据
        
        BOOL res = [db executeUpdate:sql];     //执行SQL数据
        
        if (res) {
            NSLog(@"插入成功!");
            
        }
        
        [db close];
    }

}

- (NSString *)getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    return  [NSString stringWithFormat:@"%@/db5.sqlite",documentsDirectory];
    
}


0 0