1/3. 向项目中引入FMDB库,可以使用cocoapods也可以直接手动添加,手动添加时需要自己导入对libsqlite3.tbd的依赖。
2/3. 在Main.stroryboard里在vc中添加几个按钮并对按钮的点击事件进行方法关联
3/3. 代码都在ViewController.m中,代码很简单,如下。
这里没有涉及多线程的东西,因为数据库使用情况不多,就算用了也很少用多到多线程,等以后用到了我再更新。
//
// ViewController.m
// fmdbTest
//
// Created by yuweiMac on 16/4/29.
// Copyright © 2016年 yuweiMac. All rights reserved.
//
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property (nonatomic, strong) FMDatabase *fmDB;
@end
#define TABLENAME @"info"
#define ID @"id"
#define NAME @"name"
#define AGE @"age"
#define ADDRESS @"address"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
self.fmDB = [FMDatabase databaseWithPath:[docPath stringByAppendingPathComponent:@"fmtest.db"]];
NSLog(@"%@",docPath);
}
- (IBAction)createInfoTable:(UIButton *)sender {
if ([self.fmDB open]) {
if (![self.fmDB tableExists:TABLENAME]) {
NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
NSLog(@"%@",sqlCreateTable);
BOOL res = [self.fmDB executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating info table");
} else {
NSLog(@"success to creating info table");
}
}
sender.enabled = NO;
[self.fmDB close];
}
}
- (IBAction)addData2Info:(UIButton *)sender {
if ([self.fmDB open]) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"张三", @"13", @"济南"];
NSLog(@"%@",insertSql1);
BOOL res = [self.fmDB executeUpdate:insertSql1];
NSString *insertSql2 = [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"济南"];
NSLog(@"%@",insertSql2);
BOOL res2 = [self.fmDB executeUpdate:insertSql2];
if (!( res && res2 )) {
NSLog(@"error when insert info table");
} else {
NSLog(@"success to insert info table");
}
[self.fmDB close];
}
}
- (IBAction)changeData2Info:(UIButton *)sender {
if ([self.fmDB open]) {
NSString *updateSql = [NSString stringWithFormat:
@"UPDATE '%@' SET %@ = '%@' WHERE %@ = '%@'",
TABLENAME, AGE, @"15" ,AGE, @"13"];
NSLog(@"%@",updateSql);
BOOL res = [self.fmDB executeUpdate:updateSql];
if (!res) {
NSLog(@"error when update info table");
} else {
NSLog(@"success to update info table");
}
[self.fmDB close];
}
}
- (IBAction)deleteData2Info:(UIButton *)sender {
if ([self.fmDB open]) {
NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where %@ = '%@'",
TABLENAME, NAME, @"张三"];
NSLog(@"%@",deleteSql);
BOOL res = [self.fmDB executeUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete info table");
} else {
NSLog(@"success to delete info table");
}
[self.fmDB close];
}
}
- (IBAction)queryDataOfInfo:(UIButton *)sender {
if ([self.fmDB open]) {
NSString * sql = [NSString stringWithFormat:
@"SELECT * FROM %@ ",TABLENAME];
NSLog(@"%@",sql);
FMResultSet * rs = [self.fmDB 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);
/*
FMResultSet 还有以下方法来解析数据到指定形式:
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dateForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumnName:
objectForColumnName:
*/
}
[self.fmDB close];
}
}
@end