#import "FMDB.h"
@interface CZViewController ()
- (IBAction)insertOnClick;
- (IBAction)deleteOnClick;
- (IBAction)updateOnClick;
- (IBAction)queryOnClick;
//@property (nonatomic, strong) FMDatabase *db;
@property (nonatomic, strong) FMDatabaseQueue *queue;
@end
@implementation CZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 0.獲取沙盒地址
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *sqlFilePath = [path stringByAppendingPathComponent:@"student.sqlite"];
// 1.建立一個FMDatabaseQueue對象
// 只要建立數據庫隊列對象, FMDB內部就會自動給咱們加載數據庫對象
self.queue = [FMDatabaseQueue databaseQueueWithPath:sqlFilePath];
// 2.執行操做
// 會經過block傳遞隊列中建立好的數據庫給咱們
[self.queue inDatabase:^(FMDatabase *db) {
// 編寫須要執行的代碼
// 2.1建立表(在FMDB框架中, 增長/刪除/修改/建立/銷燬都統稱爲更新)
BOOL success = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, score REAL DEFAULT 1);"];
if (success) {
NSLog(@"建立表成功");
}else
{
NSLog(@"建立表失敗");
}
}];
/*
// 1.加載數據庫對象
self.db = [FMDatabase databaseWithPath:sqlFilePath];
// 2.打開數據庫
if([self.db open])
{
NSLog(@"打開成功");
// 2.1建立表(在FMDB框架中, 增長/刪除/修改/建立/銷燬都統稱爲更新)
BOOL success = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, score REAL DEFAULT 1);"];
if (success) {
NSLog(@"建立表成功");
}else
{
NSLog(@"建立表失敗");
}
}else
{
NSLog(@"打開失敗");
}
*/
}
- (IBAction)insertOnClick
{
/*
// FMDB中能夠用?看成佔位符, 可是須要注意: 若是使用問號佔位符, 之後只能給佔位符傳遞對象
BOOL success = [self.db executeUpdate:@"INSERT INTO t_student(score, name) VALUES (?, ?);", @(20), @"Jack"];
if (success) {
NSLog(@"插入成功");
}else
{
NSLog(@"插入失敗");
}
*/
[self.queue inDatabase:^(FMDatabase *db) {
BOOL success = [db executeUpdate:@"INSERT INTO t_student(score, name) VALUES (?, ?);", @(20), @"jackson"];
if (success) {
NSLog(@"插入成功");
}else
{
NSLog(@"插入失敗");
}
}];
}
- (IBAction)deleteOnClick
{
}
- (IBAction)updateOnClick
{
/*
[self.queue inDatabase:^(FMDatabase *db) {
// 開啓事務
[db beginTransaction];
[db executeUpdate:@"UPDATE t_student SET score = 1500 WHERE name = 'zs';"];
NSArray *array = @[@"abc"];
array[1];
[db executeUpdate:@"UPDATE t_student SET score = 500 WHERE name = 'ls';"];
// 提交事務
[db commit];
}];
*/
[self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"UPDATE t_student SET score = 1500 WHERE name = 'zs';"];
NSArray *array = @[@"abc"];
// array[1];
[db executeUpdate:@"UPDATE t_student SET score = 500 WHERE name = 'ls';"];
}];
}
- (IBAction)queryOnClick
{
/*
// FMDB框架中查詢用executeQuery方法
// FMResultSet結果集, 結果集其實和tablevivew很像
FMResultSet *set = [self.db executeQuery:@"SELECT id, name, score FROM t_student;"];
while ([set next]) { // next方法返回yes表明有數據可取
int ID = [set intForColumnIndex:0];
// NSString *name = [set stringForColumnIndex:1];
NSString *name = [set stringForColumn:@"name"]; // 根據字段名稱取出對應的值
double score = [set doubleForColumnIndex:2];
NSLog(@"%d %@ %.1f", ID, name, score);
}
*/
[self.queue inDatabase:^(FMDatabase *db) {
// FMResultSet結果集, 結果集其實和tablevivew很像
FMResultSet *set = [db executeQuery:@"SELECT id, name, score FROM t_student;"];
while ([set next]) { // next方法返回yes表明有數據可取
int ID = [set intForColumnIndex:0];
// NSString *name = [set stringForColumnIndex:1];
NSString *name = [set stringForColumn:@"name"]; // 根據字段名稱取出對應的值
double score = [set doubleForColumnIndex:2];
NSLog(@"%d %@ %.1f", ID, name, score);
}
}];
}
@endsql