<<FMDB1.zip>>
http://blog.csdn.net/xyz_lmn/article/details/9312837
.h文件
#import <UIKit/UIKit.h>
#import "FMDatabase.h"
@interface FMDBViewController : UIViewController
{
// 全局变量
FMDatabase *db;
NSString *database_path;
}
@end
.m文件
//
// FMDBViewController.m
// fmdbDemo
//
// Created by 牛哲 on 15-9-13.
// Copyright (c) 2015年 牛哲 All rights reserved.
//
#import "FMDBViewController.h"
//宏定义
#define DBNAME @"personinfo.sqlite"
#define ID @"id"
#define NAME @"name"
#define AGE @"age"
#define ADDRESS @"address"
#define TABLENAME @"PERSONINFO"
@interface FMDBViewController ()
@end
@implementation FMDBViewController
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView{
[super loadView];
UIButton *openDBBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect=CGRectMake(60, 60, 200, 50);
openDBBtn.frame=rect;
[openDBBtn addTarget:self action:@selector(createTable) forControlEvents:UIControlEventTouchDown];
[openDBBtn setTitle:@"创建表" forState:UIControlStateNormal];
[self.view addSubview:openDBBtn];
UIButton *insterBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect2=CGRectMake(60, 130, 200, 50);
insterBtn.frame=rect2;
[insterBtn addTarget:self action:@selector(insertData) forControlEvents:UIControlEventTouchDown];
[insterBtn setTitle:@"插入" forState:UIControlStateNormal];
[self.view addSubview:insterBtn];
UIButton *updateBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect3=CGRectMake(60, 200, 200, 50);
updateBtn.frame=rect3;
[updateBtn addTarget:self action:@selector(updateData) forControlEvents:UIControlEventTouchDown];
[updateBtn setTitle:@"更新表" forState:UIControlStateNormal];
[self.view addSubview:updateBtn];
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect4=CGRectMake(60, 270, 200, 50);
deleteBtn.frame=rect4;
[deleteBtn addTarget:self action:@selector(deleteData) forControlEvents:UIControlEventTouchDown];
[deleteBtn setTitle:@"删除数据" forState:UIControlStateNormal];
[self.view addSubview:deleteBtn];
UIButton *selectBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect5=CGRectMake(60, 340, 200, 50);
selectBtn.frame=rect5;
[selectBtn addTarget:self action:@selector(selectData) forControlEvents:UIControlEventTouchDown];
[selectBtn setTitle:@"查询数据" forState:UIControlStateNormal];
[self.view addSubview:selectBtn];
}
//CREATE TABLE IF NOT EXISTS ‘%@‘ (‘%@‘ INTEGER PRIMARY KEY AUTOINCREMENT, ‘%@‘ TEXT, ‘%@‘ INTEGER, ‘%@‘ TEXT)
//创建表
- (void)createTable{
//sql语句
//第一步必须打开数据库
if ([db open]) {
//创建表要素 表名,字段,字段类型
NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS ‘%@‘ (‘%@‘ INTEGER PRIMARY KEY AUTOINCREMENT, ‘%@‘ TEXT, ‘%@‘ INTEGER, ‘%@‘ TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
BOOL res = [dbexecuteUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
//重点2:关闭数据库
[db close];
}
}
//@"INSERT INTO 表名 (NAME) VALUES (‘小红‘)",
//TABLENAME, NAME, AGE, ADDRESS, @"张三", @"13", @"济南"
//增null
-(void) insertData{
if ([db open]) {
//这个语句的意思是 在某张表里 插入一个数据,这个数据包含三条属性,name,age,地址 以及这三个属性的值
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘)",
TABLENAME, NAME, AGE, ADDRESS, @"张三", @"13", @"济南"];
BOOL res = [dbexecuteUpdate:insertSql1];
NSString *insertSql2 = [NSString stringWithFormat:
@"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘)",
TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"济南"];
BOOL res2 = [dbexecuteUpdate:insertSql2];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
}
}
//delete from TABLENAME where NAME = ‘张三‘,
//删
-(void) deleteData{
if ([db open]) {
//删除数据从某张表里面 删除的内容是符合以下条件的:name字段是张三的
NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where %@ = ‘%@‘",
TABLENAME, NAME, @"张三"];
BOOL res = [dbexecuteUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
}
//UPDATE TABLENAME SET name = ‘小明‘ WHERE AGE > 13"
//改
-(void) updateData{
if ([db open]) {
//修改某张表里面的这条数据
//这些数据是符合以下条件的age = 13
//将他们修改为age = 15
NSString *updateSql = [NSString stringWithFormat:
@"UPDATE ‘%@‘ SET %@ = ‘%@‘ WHERE %@ = ‘%@‘",
TABLENAME, AGE, @"18",AGE, @"13"];
BOOL res = [dbexecuteUpdate:updateSql];
if (!res) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
[db close];
}
}
//查
-(void) selectData{
if ([db open]) {
//从某张表查询数据,所有
NSString * sql = [NSString stringWithFormat:
@"SELECT * FROM %@",TABLENAME];
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
int Id = [rs intForColumn:ID];
NSString * name = [rs stringForColumn:NAME];
NSString * age = [rs stringForColumn:AGE];
NSString * address = [rs stringForColumn:ADDRESS];
NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address);
}
[db close];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
//获得程序沙盒地址
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 获取Document、temp 、cache三个目录中的第一个目录
NSString *documents = [paths objectAtIndex:0];
// 拼接成数据库所在地址
database_path = [documents stringByAppendingPathComponent:@"staff.sqlite"];
// 获得该路径下的数据库
db = [FMDatabase databaseWithPath:database_path];
// 将页面中所有的控件 按比例缩放
[self autoMakeWithView:self.view];
}
原文地址:http://10685945.blog.51cto.com/10675945/1702266