iOS开发:FMDB的使用
来源:互联网 发布:开网店软件 编辑:程序博客网 时间:2024/05/21 12:47
#import "ViewController.h"
#import "FMDatabase.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
FMDatabase * _fm;
UITableView * _tableView;
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets=YES;
self.navigationController.navigationBar.translucent=NO;
[self createTableView];
//进行初始创建数据库
//开始选取路径 路径有三种方式
1.具体文件的路径 如果不存在的话 可以自己创建
2.如果路径是空(@" ")的话,直接是在临时目录下创建一个数据库,当FMDatabase连接关闭时,数据库会销毁
3.如果路径是nil的话,那直接是在内存中创建一个数据库,当FMDatabase连接关闭时,数据库会销毁
NSString *path=[NSStringstringWithFormat:@"%@/Documents/data.db",NSHomeDirectory()];
_fm=[[FMDatabase alloc]initWithPath:path];
//打开数据库
if ([_fm open]) {
NSLog( @"打开成功");
//创建一个表 create table表明(字段名,字段名);语句不区分大小写,但是表名和字段名是区分大小写的
BOOL isSucceed=[_fm executeUpdate:@"create table stu(name,sex,age)"];
if (isSucceed) {
NSLog(@"创建成功");
}
else
{
NSLog(@"创建失败,表格已经存在");
}
}
}
-(void)createTableView
{
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height ) style:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
[self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
if (!cell) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:@"ID"];
}
//读取数据源
cell.textLabel.text=_dataArray[indexPath.row][0];
cell.detailTextLabel.text=_dataArray[indexPath.row][2];
cell.detailTextLabel.textColor=[UIColor grayColor];
return cell;
}
- (IBAction)edit:(UIBarButtonItem*)sender {
if ((sender.tag= !sender.tag)) {
[_tableView setEditing:YES];
[sender setTitle:@"完成"];
NSLog(@"-------");
}
else
{
//禁止编辑
[_tableView setEditing:NO];
NSLog(@"=======");
[sender setTitle:@"编辑"];
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s",__func__);
if (editingStyle==UITableViewCellEditingStyleDelete) {
}
else if (editingStyle==UITableViewCellEditingStyleInsert)
{
}
}
//查
- (IBAction)select:(id)sender {
//select count(*) from 表名; * 代表通配符
FMResultSet * result=[_fm executeQuery:@"select * from stu"];
//可变数组
NSMutableArray *array=[NSMutableArray array];
//集合不能使用遍历,因为集合类似链表,头结点为空
while([result next]){
//每一次读取,都是读取一行的内容,可读取的内容string类型和data类型
NSString * name=[result stringForColumn:@"name"];
NSString * age=[result stringForColumn:@"age"];
NSString * sex=[result stringForColumn:@"sex"];
//创建临时数组
[array addObject:@[name,age,sex]];
}
//显示在tableView上
//数据源追加
self.dataArray=[NSMutableArray arrayWithArray:array];
[_tableView reloadData];
/*
当我们使用完数据库以后,需要关闭数据库
在关闭数据库时候,我们程序设计,为了提高效率,会把数据库做个一个单例,在应用启动时候,就打开数据库。当进入后台时候就关闭数据库
*/
//关闭数据库
//[_fm close];
}
//改
-(IBAction)updateClick:(id)sender
{
//修改的方法应当和搜索搭配使用
//修改update表名 set字段=值 where 条件
BOOL isSucced=[_fm executeUpdate:@"update stu set age=? where age>30",[NSString stringWithFormat:@"%d",arc4random()%30]];
if (isSucced) {
NSLog(@"更新成功");
}
}
//增
- (IBAction)addClick:(id)sender {
NSLog(@"增加");
//insert into 表明 values(值,值);对应的值采用?替代,类似%@,在这里注意sql不能使用中文输入法来进行
NSArray *array=@[@"男",@"女",@"男男",@"女女",@"不男不女"];
NSString *age=[NSString stringWithFormat:@"%d",arc4random()%70+3];
[_fm executeUpdate:@"insert into stu values(?,?,?)",@"石头",array[arc4random()%5],age];
/*如何批量插入数据,当插入数据有错误,如何把之前插入的数据,全部清理?
我们需要开始一个事务,这个事务负责管理整个插入过程,如果插入需要停止,那么就撤回之前的数据,这类似在插入前有一个管理,最后进行提交
//什么时候才会用到大量的处理数据?
一般在做企业ERP应用会用到,企业应用会大量处理各种异常和订单处理
类似外勤365追踪类应用
*/
// //开启一个事务
// [fm beginTransaction];
// //在这里循环进行插入数据
// for (int i=0; i<100; i++) {
// NSString *xx=[NSString stringWithFormat:@"%d",i];
// BOOL isSucceed= [fm executeUpdate:@"insert into stu values(?,?,?)",@"三十",array[arc4random()%5],xx];
// 如果插入过程出现误差了,也就是出现错误,需要进行停止整个过程,并且撤回之前的数据
// //撤回
// if (!isSucceed) {
// [fm rollback];
// }
//
//
// }
// //如果没有错误。则进行提交
//
// BOOL isOk=[fm commit];
// if (isOk) {
// NSLog(@"完成");
// }
}
//删
- (IBAction)delectClick:(id)sender {
//delete from 表名 where 条件
BOOL isSucceed= [_fm executeUpdate:@"delete from stu where sex=? or sex=? or sex=?",@"人妖","男",@"女司机"];
if (isSucceed) {
NSLog(@"删除成功");
}
else
{
//如果删除失败,打印原因
_fm.logsErrors=YES;
NSString *str= [_fm lastErrorMessage];
NSLog(@"%@",str);
}
}
@end
- iOS开发:FMDB的使用
- iOS开发~FMDB的使用
- iOS开发 ----- FMDB使用
- iOS开发----FMDB的简单使用
- 【IOS开发】FMDB的简单使用
- iOS开发-FMDB的基本使用
- iOS开发--FMDB的使用
- IOS开发基础:FMDB框架的使用
- iOS开发FMDB的简单使用
- iOS FMDB的使用
- iOS FMDB的使用
- Ios fmdb的使用
- iOS FMDB的使用
- iOS开发中使用FMDB
- iOS开发中使用FMDB的网文iOS 使用FMDB进行数据库操作
- 【ios开发】使用FMDB封装类的sqlite数据库操作
- iOS开发之Sqlite 第三方库FMDB的使用
- iOS开发——数据库(FMDB框架的使用)
- CMD安装卸载apk及apk打包发布图解
- TXT小说朗读正式版
- 加速Android Studio/Gradle构建
- 周赛4
- Xcode工程中的Project和Target区别
- iOS开发:FMDB的使用
- 高斯过程(Gaussian Process)
- 获取对象在内存中计算后的样式
- UVA101-The Blocks Problem
- 正则表达式-数字每隔3位加入逗号
- 快学Scala习题解答—第十章 特质
- C++内存地址分配和内存区划分简介
- SharedPreferences 存储
- CODE COMPLETE 2e Chapter 6