你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS數據庫操作fmdb1: FMDB數據庫使用

iOS數據庫操作fmdb1: FMDB數據庫使用

編輯:關於IOS

FMDatabase 數據庫
1.下載FMDatabase 導入下載裡面的src文件,刪了fmdb.m文件 2.引入framework:libsqlite3.dylib 3.引入頭文件 #import "FMDatabase.h” 數據庫文件要兼容iPhone4和iPhone5需要放在Document或Library目錄中,先判斷Document/Library中數據庫文件(一般是.sqlite後綴)是否存在,如果不存在則從工程(bundle)中把數據庫文件拷貝到Document中或Library中。
以下是我是我工程中用過的一個數據庫操作代碼,記下來,免得忘記了: [cpp] view plaincopyprint?

  1. //數據庫操作,1代表增減,2代表刪除,3,代表查詢,4,代表修改
  2. -(void)operateDB:(NSInteger)num andRow:(NSInteger)row status:(NSInteger)status{
  3. self.deleteOld = YES;
  4. self.dbFile = @"shopCar_db.sqlite";
  5. NSFileManager *manager = [NSFileManager defaultManager];
  6. NSString *dbPath = [Utilities documentsPath:self.dbFile];
  7. //沒存在則拷貝到document
  8. if (![manager fileExistsAtPath:dbPath]) {
  9. NSString *bundlePath = [Utilities bundlePath:self.dbFile];
  10. if ([manager copyItemAtPath:bundlePath toPath:dbPath error:nil]) {
  11. NSLog(@"copy ok...");
  12. }
  13. else
  14. {
  15. NSLog(@"copy error...");
  16. }
  17. }
  18. else{
  19. if (self.deleteOld) {
  20. NSLog(@"delete old and create new..");
  21. NSString *bundlePath = [Utilities bundlePath:self.dbFile];
  22. [manager removeItemAtPath:dbPath error:nil];
  23. [manager copyItemAtPath:bundlePath toPath:dbPath error:nil];
  24. }
  25. else
  26. {
  27. NSLog(@"do nothing...");
  28. }
  29. }
  30. FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
  31. NSLog(@"begin to db...");
  32. if ([db open]) {
  33. if(status == 1){
  34. NSLog(@"增減.......");
  35. }
  36. if(status == 2){
  37. NSString *sql = [NSString stringWithFormat:@"delete from shopCar_tb where id = %i",row];
  38. [db executeUpdate:sql];
  39. }
  40. if(status == 3){
  41. NSLog(@"查詢........");
  42. }
  43. if(status == 4){
  44. NSString *sql = [NSString stringWithFormat:@"UPDATE shopCar_tb SET num = '%d' WHERE id = %d",num,row];
  45. [db executeUpdate:sql];
  46. }
  47. NSString *sql = @"select * from shopCar_tb";
  48. FMResultSet *result = [db executeQuery:sql];
  49. _mutableArrayData = [[NSMutableArray alloc] init];
  50. [_mutableArrayData removeAllObjects];
  51. [_mutableDictionayData removeAllObjects];
  52. while ([result next]) {
  53. _mutableDictionayData = [[NSMutableDictionary alloc] init];
  54. NSString *sid = [result stringForColumn:@"id"];
  55. [_mutableDictionayData setObject:sid forKey:@"id"];
  56. NSString *name = [result stringForColumn:@"name"];
  57. [_mutableDictionayData setObject:name forKey:@"name"];
  58. NSString *gid = [result stringForColumn:@"gid"];
  59. [_mutableDictionayData setObject:gid forKey:@"gid"];
  60. NSString *num = [result stringForColumn:@"num"];
  61. [_mutableDictionayData setObject:num forKey:@"num"];
  62. NSLog(@"num = %i",[num intValue]);
  63. NSString *price = [result stringForColumn:@"price"];
  64. [_mutableDictionayData setObject:price forKey:@"price"];
  65. NSString *path = [result stringForColumn:@"path"];
  66. [_mutableDictionayData setObject:path forKey:@"path"];
  67. [_mutableArrayData addObject:_mutableDictionayData];
  68. }
  69. }
  70. [db close];
  71. NSLog(@"end db...");
  72. }
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved