您的位置:首页 > 编程语言

FMDatabase的简单使用-代码举例

2015-09-08 21:30 344 查看
#import "RootViewController.h"

#import "FMDatabase.h"

@interfaceRootViewController (){
FMDatabase *_dataBase;
}

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {

// Custom initialization
}

returnself;
}

- (void)viewDidLoad
{

[superviewDidLoad];

NSLog(@"%@",NSHomeDirectory());

// Do any additional setup after loading the view.

[selfcreatButtons];
}
-(void)creatButtons{

NSArray *titleArray=@[@"创建数据库",@"创建表",@"增加",@"改动",@"查询",@"删除"];
for (int i=0; i<titleArray.count;
i++) {

UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeSystem];

[btn setTitle:titleArray[i]forState:UIControlStateNormal];
btn.frame=CGRectMake((i/3)*100+40,
(i%3)*100+40,100,
30);
btn.tag=100+i;

[btn addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
}
-(void)click:(UIButton *)button{

if (button.tag==100) {

//创建数据库,并打开数据库(因为要创建表)

//通常情况下,数据库是存储在手机沙盒目录下的,所以要首先找到沙盒路径

NSString *sandBoxPath=[NSHomeDirectory()stringByAppendingPathComponent:@"Documents/zhulei.db"];//名字自己起

//根据这个路径,创建数据库
_dataBase=[[FMDatabasealloc]initWithPath:sandBoxPath];
//打开数据库

//[_dataBase open];
BOOL open=[_dataBaseopen];
if (open) {
NSLog(@"数据库创建成功");
}
}elseif (button.tag==101){



//创建数据库下的表

//表里面存在一个主键和要储存的数据的属性

//创建表的sql语句

//students :表名

//username varchar(256),password varchar(256),sex varchar(256) :属性及大小

NSString *creatTableString=@"create table if not exists students(id integer primary key autoincrement,username varchar(256),password
varchar(256),sex varchar(256))";

//看表单是否创建成功
BOOL table=[_dataBaseexecuteUpdate:creatTableString];
if (table) {
NSLog(@"表格创建成功");
}
}elseif (button.tag==102){



//往students里面增加数据

//insert的sql语句

NSString *insertStr=@"insert into students(username,password,sex) values(?,?,?)";
BOOL insert=[_dataBaseexecuteUpdate:insertStr,@"朱磊",@"123456",@"男"];
if (insert) {
NSLog(@"插入成功");
}
}elseif (button.tag==104){


//查询数据

//查询的SQL语句 select

//表中的id是表自动生成的

NSString *selectStr=@"select *from students where id=1";

//查询要从另外一个里面去查 FMResultSet

//FMResultSet -->可以理解成一个数组
FMResultSet *set=[_dataBaseexecuteQuery:selectStr];

//遍历,查询到id=1的那条数据
while ([set
next]) {
NSString *username=[set
stringForColumn:@"username"];
NSString *password=[set
stringForColumn:@"password"];
NSString *sex=[set
stringForColumn:@"sex"];
NSLog(@"username=%@ password=%@ sex=%@",username,password,sex);
}
}elseif (button.tag==103){



//改动数据 update

//改动的SQL语句

NSString *updateStr=@"update students set username='马晨星',password='1234',sex='woman'
where id=1";
BOOL updata=[_dataBaseexecuteUpdate:updateStr];
if (updata) {
NSLog(@"改动成功");
}


}elseif (button.tag==105){



//删除delete

//删除的SQL语句

NSString *deleteStr=@"delete from students where id=1";
BOOL delete=[_dataBaseexecuteUpdate:deleteStr];
if (delete) {
NSLog(@"删除成功");
}
}
}
- (void)didReceiveMemoryWarning
{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: