Guest User

Untitled

a guest
May 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. - (void)createFMDBSqlDB
  2. {
  3. NSLog(@"%s", __func__);
  4. NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).firstObject;
  5. NSString *sqlFilePath = [docPath stringByAppendingString:@"user.sqlite"];
  6. NSFileManager *fileManager = [NSFileManager defaultManager];
  7.  
  8. if ([fileManager fileExistsAtPath:sqlFilePath]) {
  9. fmdbsql_ = [FMDatabase databaseWithPath:sqlFilePath];
  10. if ([fmdbsql_ open]) {
  11. NSString *sql = @"CREATE TABLE if not exists 'User' ('id' INTERGER PRIMARY KEY NOT NULL, 'name' VARCHAR(30), 'password' VARCHAR(30))";
  12. BOOL res = [fmdbsql_ executeUpdate:sql];
  13. if (!res) {
  14. NSLog(@"error when creating db table");
  15. }
  16. else {
  17. NSLog(@"success to creating db table");
  18. }
  19. //[db close];
  20. }
  21. }
  22. else {
  23. BOOL res = [fileManager createFileAtPath:sqlFilePath contents:nil attributes:nil];
  24. NSLog(@"创建文件: %@", res == YES ? @"成功":@"失败");
  25. }
  26. }
  27.  
  28. - (void)insertFMDBTable
  29. {
  30. if ([fmdbsql_ open]) {
  31. NSString *sql = @"insert into User (id, name, password) values(?,?,?)";
  32. NSInteger idInt = random()%1000;
  33. NSString * name = [NSString stringWithFormat:@"zichen_%ld", (long)idInt];
  34. NSString * password = [NSString stringWithFormat:@"zi_%ld", (long)idInt];
  35. BOOL res = [fmdbsql_ executeUpdate:sql, [NSNumber numberWithInteger:idInt], name, password];
  36. if (res) {
  37. NSLog(@"insert success");
  38. }
  39. else {
  40. NSLog(@"insert error");
  41. }
  42. }
  43. else {
  44. [self createFMDBSqlDB];
  45. }
  46. }
  47.  
  48. - (void)selectFMDB
  49. {
  50. if ([fmdbsql_ open]) {
  51. NSString *sql = @"select * from User";
  52. FMResultSet * rs = [fmdbsql_ executeQuery:sql];
  53. while ([rs next]) {
  54. int userID = [rs intForColumnIndex:0];
  55. int userid = [rs intForColumn:@"id"];
  56.  
  57. NSString *name = [rs stringForColumn:@"name"];
  58. NSString * password = [rs stringForColumn:@"password"];
  59.  
  60. NSLog(@"id %ld-%ld, name-%@, passwor-%@", (long)userID, (long)userid, name, password);
  61. }
  62. }
  63. else {
  64. [self createFMDBSqlDB];
  65. }
  66. }
  67.  
  68. - (void)deleteAllFMDBTable
  69. {
  70. if ([fmdbsql_ open]) {
  71. NSString *sql = @"delete from User";
  72. BOOL res = [fmdbsql_ executeUpdate:sql];
  73. if (res) {
  74. NSLog(@"delete All success");
  75. }
  76. else {
  77. NSLog(@"delete error");
  78. }
  79. }
  80. else {
  81. [self createFMDBSqlDB];
  82. }
  83. }
  84.  
  85. - (void)mutilThread
  86. {
  87. NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).firstObject;
  88. NSString *sqlFilePath = [docPath stringByAppendingString:@"user.sqlite"];
  89. FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:sqlFilePath];
  90.  
  91. dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
  92. dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);
  93. dispatch_async(q1, ^{
  94. for (int i = 0; i < 15; i++) {
  95. [queue inDatabase:^(FMDatabase * _Nonnull db) {
  96. NSString *sql = @"insert into User (id, name, password) values(?,?,?)";
  97. NSInteger idInt = random()%1000;
  98. NSString * name = [NSString stringWithFormat:@"zichen_%ld", (long)idInt];
  99. NSString * password = [NSString stringWithFormat:@"zi_%ld", (long)idInt];
  100. BOOL res = [fmdbsql_ executeUpdate:sql, [NSNumber numberWithInteger:idInt], name, password];
  101. if (res) {
  102. NSLog(@"insert success");
  103. }
  104. else {
  105. NSLog(@"insert error");
  106. }
  107. }];
  108. }
  109. });
  110.  
  111. dispatch_async(q2, ^{
  112. for (int i = 0; i < 15; i++) {
  113. [queue inDatabase:^(FMDatabase * _Nonnull db) {
  114. NSString *sql = @"insert into User (id, name, password) values(?,?,?)";
  115. NSInteger idInt = random()%1000;
  116. NSString * name = [NSString stringWithFormat:@"zichen_%ld", (long)idInt];
  117. NSString * password = [NSString stringWithFormat:@"zi_%ld", (long)idInt];
  118. BOOL res = [fmdbsql_ executeUpdate:sql, [NSNumber numberWithInteger:idInt], name, password];
  119. if (res) {
  120. NSLog(@"insert success");
  121. }
  122. else {
  123. NSLog(@"insert error");
  124. }
  125. }];
  126. }
  127. });
  128. }
Add Comment
Please, Sign In to add comment