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.
}
#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.
}
相关文章推荐
- Java帮助文档的制作 - javadoc
- PHP基础语法实例
- Django中因south导致Syncdb命令无法建立表的问题
- 第一章 C#概论
- 完整版单例代码
- Spring定时任务的几种实现
- 第51讲:Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用
- django 1.8 官方文档翻译: 2-5-7 自定义查找
- 搜索到的Github
- Spring part 3:AOP中的代理
- mongo如何查看性能 debug
- java中的任务调度
- IIS对Asp.Net WebForm和Asp.Net Mvc的处理通用部分
- 【深入ASP.NET原理系列】--ASP.NET请求管道对Asp.Net WebForm和Asp.Net Mvc的处理通用部分
- 心形代码
- JAVA中快捷键的使用
- Struts2.x和Struts1.x的区别
- org.osgi.framework.BundleException: Could not find bundle: org.eclipse.equinox.console错误解决办法
- cassandra cpp driver 2.1 阅读摘要
- window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法