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];
}
- ios运用fmdb建SQLite数据库
- iOS FMDB操作sqlite数据库
- iOS SQLite、CoreData、FMDB数据库详解
- iOS中 FMDB第三方SQLite数据库
- iOS-FMDB事务操作SQLite数据库
- iOS-FMDB事务操作SQLite数据库
- iOS FMDB Sqlite
- IOS Sqlite使用FMDB
- 【ios开发】使用FMDB封装类的sqlite数据库操作
- ios sqlite数据库,采用第三方框架FMDB【转】
- ios sqlite 第三方FMDB框架操作数据库
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS中 FMDB第三方SQLite数据库 UI_20
- iOS中 用FMDB封装一个SQLite数据库
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS开发 - 第06篇 - 实用技术 - 02 - 数据库SQLite & FMDB
- python类知识
- Introduction on Using Hamcrest Unit Test Framework
- axis2-----5
- 第一个项目(五)添加跳跃动作
- axis2---5--2
- ios运用fmdb建SQLite数据库
- 在sed命令中使用外部变量
- 【容斥原理+欧拉函数】 HDOJ 1695 GCD
- Lua string库
- qtwebkit插件
- Java基础视频教程-异常,包
- axis--0000
- kernel 框架
- 开源ext2read代码走读之-扩展分区与逻辑分区说明及如何读取扩展分区的分区表(EBR)