IOS database operation fmdb1: used by the FMDB Database

Source: Internet
Author: User

IOS database operation fmdb1: used by the FMDB Database
FMDatabase
1. download the src file in the FMDatabase import and download and delete the fmdb. m file 2. introduce framework: libsqlite3.dylib3. introduce the header file # import "FMDatabase. h "database files must be compatible with iPhone 4 and iPhone 5 in the Document or Library directory. First, judge the database files in Document/Library (generally. sqlite suffix). If not, copy the database file from the project (bundle) to the Document or Library.
The following is a database operation code I used in my project. Write it down to avoid forgetting it:[Cpp]View plaincopyprint?

  1. // Database operation. 1 indicates increase or decrease, 2 indicates deletion, 3 indicates query, 4 indicates modification.
  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 defamanager manager];
  6. NSString * dbPath = [Utilities documentsPath: self. dbFile];
  7. // Copy the object to the document if it does not exist
  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 (@ "add or remove .......");
  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 (@ "query ........");
  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. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.