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

 

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

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

  2. 获得存放数据库文件的沙盒地址
  3. [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. +(NSString *)databaseFilePath  
    2. {  
    3. NSArray *filePath   
    4. = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    5. NSString *documentPath = [filePath objectAtIndex:0];  
    6. NSLog(@"%@",filePath);  
    7. NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"db.sqlite"];  
    8. return dbFilePath;   
    9. }  

    1. 创建数据库的操作
    2. [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. +(void)creatDatabase  
      2. {  
      3. db = [[FMDatabase databaseWithPath:[self databaseFilePath]] retain];  
      4. }  

    3. 创建表
    4. [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. +(void)creatTable  
      2. {  
      3. //先判断数据库是否存在,如果不存在,创建数据库  
      4. if (!db) {  
      5. [self creatDatabase];  
      6. }  
      7. //判断数据库是否已经打开,如果没有打开,提示失败   
      8. if (![db open]) {  
      9. NSLog(@"数据库打开失败");  
      10. return;  
      11. }  
      12.   
      13. //为数据库设置缓存,提高查询效率   
      14. [db setShouldCacheStatements:YES];  
      15.    
      16. //判断数据库中是否已经存在这个表,如果不存在则创建该表  
      17. if(![db tableExists:@"people"])  
      18. {  
      19. [db executeUpdate:@"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) "];  
      20.    
      21.    
      22. NSLog(@"创建完成");  
      23. }  
      24.    
      25. }  

    5. 增加表数据
    6. [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. +(void)insertPeople:(People *)aPeople  
      2. {  
      3. if (!db) {  
      4. [self creatDatabase];  
      5. }  
      6.    
      7. if (![db open]) {  
      8. NSLog(@"数据库打开失败");  
      9. return;  
      10. }  
      11.    
      12. [db setShouldCacheStatements:YES];  
      13.    
      14. if(![db tableExists:@"people"])  
      15. {  
      16. [self creatTable];  
      17. }  
      18. //以上操作与创建表是做的判断逻辑相同  
      19. //现在表中查询有没有相同的元素,如果有,做修改操作   
      20. FMResultSet *rs = [db executeQuery:@"select * from people where people_id = ?",[NSString stringWithFormat:@"%d",aPeople.peopleID]];  
      21. if([rs next])   
      22. {  
      23. NSLog(@"dddddslsdkien");  
      24. [db executeUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSString stringWithFormat:@"%d",aPeople.age]];  
      25. }  
      26. //向数据库中插入一条数据   
      27. else{  
      28. [db executeUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSString stringWithFormat:@"%d",aPeople.age]];  
      29. }  
      30.    
      31.    
      32. }  

    7. 删除数据
    8. [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. +(void)deletePeopleByID:(int)ID  
      2. {  
      3. if (!db) {  
      4. [self creatDatabase];  
      5. }  
      6.    
      7. if (![db open]) {  
      8. NSLog(@"数据库打开失败");  
      9. return;  
      10. }  
      11.    
      12. [db setShouldCacheStatements:YES];  
      13.    
      14. //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return  
      15. if(![db tableExists:@"people"])  
      16. {  
      17. return;  
      18. }  
      19. //删除操作   
      20. [db executeUpdate:@"delete from people where people_id = ?", [NSString stringWithFormat:@"%d",ID]];  
      21.    
      22. [db close];  
      23. }  

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

    10. 查询
    11. [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. +(NSArray *)getAllPeople  
      2. {  
      3.    
      4. if (!db) {  
      5. [self creatDatabase];  
      6. }  
      7.    
      8. if (![db open]) {  
      9. NSLog(@"数据库打开失败");  
      10. return nil;  
      11. }  
      12.    
      13. [db setShouldCacheStatements:YES];  
      14.    
      15. if(![db tableExists:@"people"])  
      16. {  
      17. return nil;  
      18. }  
      19.   
      20. //定义一个可变数组,用来存放查询的结果,返回给调用者   
      21. NSMutableArray *peopleArray = [[NSMutableArray alloc] initWithArray:0];  
      22. //定义一个结果集,存放查询的数据  
      23. FMResultSet *rs = [db executeQuery:@"select * from people"];  
      24. //判断结果集中是否有数据,如果有则取出数据  
      25. while ([rs next]) {  
      26. People *aPeople = [[People alloc] init];  
      27.    
      28. aPeople.peopleID = [rs intForColumn:@"people_id"];  
      29. aPeople.name = [rs stringForColumn:@"name"];  
      30. aPeople.age = [rs intForColumn:@"age"];  
      31. //将查询到的数据放入数组中。   
      32. [peopleArray addObject:aPeople];  
      33. }  
      34. return [peopleArray autorelease];  
      35. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值