#import "FMDatabase.h" #import "FMDatabaseQueue.h"
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [doc stringByAppendingPathComponent:@"user.sqlite"]; self.dbPath = path; NSLog(@"dbPath---%@", path);
// 建表
- (void)createTable {
NSLog(@"%s", __func__);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:self.dbPath] == NO) {
// create it
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"CREATE TABLE 'User' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'name' VARCHAR(30), 'password' VARCHAR(30))";
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close];
} else {
NSLog(@"error when open db");
}
}
}
// 插入数据
- (void)insertData {
NSLog(@"%s", __func__);
static int idx = 1;
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"insert into user (name, password) values(?, ?) ";
NSString *name = [NSString stringWithFormat:@"ZL%d", idx++];
BOOL res = [db executeUpdate:sql, name, @"girl"];
if (!res) {
NSLog(@"error to insert data");
} else {
NSLog(@"success to insert data");
ZLTestModel *model = [ZLTestModel modelWith:name id:idx];
[self.userArr addObject:model];
[self.tableView reloadData];
}
[db close];
}
}
// 更新数据
- (void)updateData {
NSLog(@"%s", __func__);
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"UPDATE USER SET id = ? WHERE name = ?";
BOOL res = [db executeUpdate:sql, @"1", @"zl"];
if (!res) {
NSLog(@"error to UPDATE data");
} else {
NSLog(@"success to UPDATE data");
[self queryData];
}
[db close];
}
}
// 删除数据
- (void)deleteData {
NSLog(@"%s", __func__);
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"delete from user";
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error to delete db data");
} else {
NSLog(@"success to delete db data");
[self.userArr removeAllObjects];
[self.tableView reloadData];
}
[db close];
}
}
// 查询数据
- (void)queryData {
NSLog(@"%s", __func__);
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"select *from user";
FMResultSet *rs = [db executeQuery:sql];
while ([rs next]) {
int userId = [rs intForColumn:@"id"];
NSString *name = [rs stringForColumn:@"name"];
NSString *pass = [rs stringForColumn:@"password"];
NSLog(@"user id = %d, name = %@, pass = %@", userId, name, pass);
ZLTestModel *model = [ZLTestModel modelWith:name id:userId];
[self.userArr addObject:model];
[self.tableView reloadData];
}
[db close];
}
}
- (void)multithread {
NSLog(@"%s", __func__);
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath];
dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);
dispatch_async(q1, ^{
for (int i = 0; i < 100; ++i) {
[queue inDatabase:^(FMDatabase *db) {
NSString *sql = @"insert into user (name, password) values(?, ?) ";
NSString *name = [NSString stringWithFormat:@"queue111 %d", i];
BOOL res = [db executeUpdate:sql, name, @"boy"];
if (!res) {
NSLog(@"error to add db data: %@", name);
} else {
NSLog(@"success to add db data: %@", name);
}
}];
}
});
dispatch_async(q2, ^{
for (int i = 0; i < 100; ++i) {
[queue inDatabase:^(FMDatabase *db) {
NSString *sql = @"insert into user (name, password) values(?, ?) ";
NSString *name = [NSString stringWithFormat:@"queue222 %d", i];
BOOL res = [db executeUpdate:sql, name, @"boy"];
if (!res) {
NSLog(@"error to add db data: %@", name);
} else {
NSLog(@"success to add db data: %@", name);
}
}];
}
});
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有