Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
FMDB operation
- (void)createFMDBSqlDB
{
NSLog(@"%s", __func__);
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).firstObject;
NSString *sqlFilePath = [docPath stringByAppendingString:@"user.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:sqlFilePath]) {
fmdbsql_ = [FMDatabase databaseWithPath:sqlFilePath];
if ([fmdbsql_ open]) {
NSString *sql = @"CREATE TABLE if not exists 'User' ('id' INTERGER PRIMARY KEY NOT NULL, 'name' VARCHAR(30), 'password' VARCHAR(30))";
BOOL res = [fmdbsql_ executeUpdate:sql];
if (!res) {
NSLog(@"error when creating db table");
}
else {
NSLog(@"success to creating db table");
}
//[db close];
}
}
else {
BOOL res = [fileManager createFileAtPath:sqlFilePath contents:nil attributes:nil];
NSLog(@"创建文件: %@", res == YES ? @"成功":@"失败");
}
}
- (void)insertFMDBTable
{
if ([fmdbsql_ open]) {
NSString *sql = @"insert into User (id, name, password) values(?,?,?)";
NSInteger idInt = random()%1000;
NSString * name = [NSString stringWithFormat:@"zichen_%ld", (long)idInt];
NSString * password = [NSString stringWithFormat:@"zi_%ld", (long)idInt];
BOOL res = [fmdbsql_ executeUpdate:sql, [NSNumber numberWithInteger:idInt], name, password];
if (res) {
NSLog(@"insert success");
}
else {
NSLog(@"insert error");
}
}
else {
[self createFMDBSqlDB];
}
}
- (void)selectFMDB
{
if ([fmdbsql_ open]) {
NSString *sql = @"select * from User";
FMResultSet * rs = [fmdbsql_ executeQuery:sql];
while ([rs next]) {
int userID = [rs intForColumnIndex:0];
int userid = [rs intForColumn:@"id"];
NSString *name = [rs stringForColumn:@"name"];
NSString * password = [rs stringForColumn:@"password"];
NSLog(@"id %ld-%ld, name-%@, passwor-%@", (long)userID, (long)userid, name, password);
}
}
else {
[self createFMDBSqlDB];
}
}
- (void)deleteAllFMDBTable
{
if ([fmdbsql_ open]) {
NSString *sql = @"delete from User";
BOOL res = [fmdbsql_ executeUpdate:sql];
if (res) {
NSLog(@"delete All success");
}
else {
NSLog(@"delete error");
}
}
else {
[self createFMDBSqlDB];
}
}
- (void)mutilThread
{
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).firstObject;
NSString *sqlFilePath = [docPath stringByAppendingString:@"user.sqlite"];
FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:sqlFilePath];
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 < 15; i++) {
[queue inDatabase:^(FMDatabase * _Nonnull db) {
NSString *sql = @"insert into User (id, name, password) values(?,?,?)";
NSInteger idInt = random()%1000;
NSString * name = [NSString stringWithFormat:@"zichen_%ld", (long)idInt];
NSString * password = [NSString stringWithFormat:@"zi_%ld", (long)idInt];
BOOL res = [fmdbsql_ executeUpdate:sql, [NSNumber numberWithInteger:idInt], name, password];
if (res) {
NSLog(@"insert success");
}
else {
NSLog(@"insert error");
}
}];
}
});
dispatch_async(q2, ^{
for (int i = 0; i < 15; i++) {
[queue inDatabase:^(FMDatabase * _Nonnull db) {
NSString *sql = @"insert into User (id, name, password) values(?,?,?)";
NSInteger idInt = random()%1000;
NSString * name = [NSString stringWithFormat:@"zichen_%ld", (long)idInt];
NSString * password = [NSString stringWithFormat:@"zi_%ld", (long)idInt];
BOOL res = [fmdbsql_ executeUpdate:sql, [NSNumber numberWithInteger:idInt], name, password];
if (res) {
NSLog(@"insert success");
}
else {
NSLog(@"insert error");
}
}];
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment