iOS关于SQLite数据库的操作(使用第三方类库FMDB)

  1. 首先要先导入第三方类库FMdatabase

  2. 获得存放数据库文件的沙盒地址
  3. +(NSString *)databaseFilePath
    {
    NSArray *filePath 
    = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [filePath objectAtIndex:0];
    NSLog(@"%@",filePath);
    NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"db.sqlite"];
    return dbFilePath; 
    }

    1. 创建数据库的操作
    2. +(void)creatDatabase
      {
      db = [[FMDatabase databaseWithPath:[self databaseFilePath]] retain];
      }

    3. 创建表
    4. +(void)creatTable
      {
      //先判断数据库是否存在,如果不存在,创建数据库
      if (!db) {
      [self creatDatabase];
      }
      //判断数据库是否已经打开,如果没有打开,提示失败 
      if (![db open]) {
      NSLog(@"数据库打开失败");
      return;
      }
      
      //为数据库设置缓存,提高查询效率 
      [db setShouldCacheStatements:YES];
       
      //判断数据库中是否已经存在这个表,如果不存在则创建该表
      if(![db tableExists:@"people"])
      {
      [db executeUpdate:@"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) "];
       
       
      NSLog(@"创建完成");
      }
       
      }

    5. 增加表数据
    6. +(void)insertPeople:(People *)aPeople
      {
      if (!db) {
      [self creatDatabase];
      }
       
      if (![db open]) {
      NSLog(@"数据库打开失败");
      return;
      }
       
      [db setShouldCacheStatements:YES];
       
      if(![db tableExists:@"people"])
      {
      [self creatTable];
      }
      //以上操作与创建表是做的判断逻辑相同
      //现在表中查询有没有相同的元素,如果有,做修改操作 
      FMResultSet *rs = [db executeQuery:@"select * from people where people_id = ?",[NSString stringWithFormat:@"%d",aPeople.peopleID]];
      if([rs next]) 
      {
      NSLog(@"dddddslsdkien");
      [db executeUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSString stringWithFormat:@"%d",aPeople.age]];
      }
      //向数据库中插入一条数据 
      else{
      [db executeUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSString stringWithFormat:@"%d",aPeople.age]];
      }
       
       
      }

    7. 删除数据
    8. +(void)deletePeopleByID:(int)ID
      {
      if (!db) {
      [self creatDatabase];
      }
       
      if (![db open]) {
      NSLog(@"数据库打开失败");
      return;
      }
       
      [db setShouldCacheStatements:YES];
       
      //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return
      if(![db tableExists:@"people"])
      {
      return;
      }
      //删除操作 
      [db executeUpdate:@"delete from people where people_id = ?", [NSString stringWithFormat:@"%d",ID]];
       
      [db close];
      }

    9. 修改操作与增加操作的步骤一致

    10. 查询
    11. +(NSArray *)getAllPeople
      {
       
      if (!db) {
      [self creatDatabase];
      }
       
      if (![db open]) {
      NSLog(@"数据库打开失败");
      return nil;
      }
       
      [db setShouldCacheStatements:YES];
       
      if(![db tableExists:@"people"])
      {
      return nil;
      }
      
      //定义一个可变数组,用来存放查询的结果,返回给调用者 
      NSMutableArray *peopleArray = [[NSMutableArray alloc] initWithArray:0];
      //定义一个结果集,存放查询的数据
      FMResultSet *rs = [db executeQuery:@"select * from people"];
      //判断结果集中是否有数据,如果有则取出数据
      while ([rs next]) {
      People *aPeople = [[People alloc] init];
       
      aPeople.peopleID = [rs intForColumn:@"people_id"];
      aPeople.name = [rs stringForColumn:@"name"];
      aPeople.age = [rs intForColumn:@"age"];
      //将查询到的数据放入数组中。 
      [peopleArray addObject:aPeople];
      }
      return [peopleArray autorelease];
      }
      

      转自:http://www.th7.cn/Program/IOS/201211/110817.shtml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值