iOS中FMDB数据库之增删改查使用实例


当前第2页 返回上一页

#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];
  }
}

multithread:

- (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);
        }
      }];
    }
  });
}



标签:SQLite

返回前面的内容

相关阅读 >>

Sqlite教程(一):Sqlite数据库介绍

javascript封装的Sqlite操作类实例

android平台的sql注入漏洞浅析(一条短信控制你的手机)

Sqlite教程(三):数据表和视图简介

nodejs中安装ghost出错的原因及解决方法

Sqlite快速入门指南

c#Sqlite数据库的搭建及使用技巧

Sqlite教程(十一):临时文件

Sqlitestudio优雅调试android手机数据库Sqlite(推荐)

python sqlalchemy 中的engine详解

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...