iOS資料庫操作fmdb1: FMDB資料庫使用

來源:互聯網
上載者:User

iOS資料庫操作fmdb1: FMDB資料庫使用
FMDatabase 資料庫
1.下載FMDatabase 匯入下載裡面的src檔案,刪了fmdb.m檔案2.引入framework:libsqlite3.dylib3.引入標頭檔 #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. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.