iOS data storage mode Fmdb

Source: Internet
Author: User

This article reprinted to http://blog.csdn.net/chen505358119/article/details/9289489Category: iOS2013-07-10 14:05 2471 People read comments (0) favorite reports

Download the Fmdb's original code from the Web, drag it into the project, and add Libsqlite3.dylib in link Binary with libraries to use the

The implementation is as follows:

[Plain]View Plaincopyprint?
  1. #import "ViewController.h"
  2. #import "FMDatabase.h"
  3. #import "FMDatabaseAdditions.h"
  4. #import "FMDatabaseQueue.h"
  5. @interface Viewcontroller ()
  6. @end
  7. @implementation Viewcontroller
  8. -(void) viewdidload
  9. {
  10. [Super Viewdidload];
  11. Additional setup after loading the view, typically from a nib.
  12. }
  13. -(void) didreceivememorywarning
  14. {
  15. [Super didreceivememorywarning];
  16. Dispose of any resources the can be recreated.
  17. }
  18. -(void) Dealloc {
  19. [_nametext release];
  20. [_agetext release];
  21. [_sextext release];
  22. [_showlabel release];
  23. [Super Dealloc];
  24. }
  25. -(void) Viewdidunload {
  26. [Self setnametext:nil];
  27. [Self setagetext:nil];
  28. [Self setsextext:nil];
  29. [Self setshowlabel:nil];
  30. [Super Viewdidunload];
  31. }
  32. Save
  33. -(Ibaction) SAVEBTN: (ID) Sender {
  34. fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
  35. if (![ Database Open]) {
  36. NSLog (@ "Open database Failed");
  37. Return
  38. }
  39. if (![ Database tableexists:@ "User"]) {
  40. [Database executeupdate:@ "CREATE TABLE User (ID integer primary key autoincrement not null,name text,age integer,sex text) "];
  41. }
  42. BOOL insert=[database executeupdate:@ "insert into User (Name,age,sex) VALUES (?,?,?)", _nametext.text,[nsnumber Numberwithinteger:[_agetext.text Integervalue]],_sextext.text];
  43. The following comments also enable the insertion of data, except that it is passed into the dictionary
  44. nsmutabledictionary* Argsdict=[nsmutabledictionary Dictionary];
  45. [Argsdict setobject:_nametext.text forkey:@ "name"];
  46. [Argsdict setobject:_agetext.text forkey:@ "age"];
  47. [Argsdict setobject:_sextext.text forkey:@ "Sex"];
  48. BOOL insert=[database executeupdate:@ "insert into User (Name,age,sex) VALUES (: Name,:age,:sex)" Withparameterdiction ARY:ARGSDICT];
  49. if (insert) {
  50. uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "saved successfully" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
  51. [Alert show];
  52. [Alert release];
  53. }else{
  54. NSLog (@ "Insert failed");
  55. }
  56. [Database Close];
  57. This section of the note below is also the creation of a table except that it is multithreaded secure, and the above is unsafe in multiple threads
  58. fmdatabasequeue* queue=[fmdatabasequeue databasequeuewithpath:[self DatabasePath];
  59. [Queue indatabase:^ (fmdatabase* database) {
  60. if (![ Database tableexists:@ "User"]) {
  61. [Database executeupdate:@] CREATE TABLE user (ID integer primary key autoincrement not null,name Text,age int Eger,sex text) "];
  62. //        }
  63. BOOL insert=[database executeupdate:@ "insert into User (Name,age,sex) VALUES (?,?,?)", _nametext.text,[nsnumber N Umberwithinteger:[_agetext.text Integervalue]],_sextext.text];
  64. if (insert) {
  65. uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "saved successfully" Delegate:nil cancelbuttontitle:n Il otherbuttontitles:@ "OK", nil];
  66. [Alert show];
  67. [Alert release];
  68. }else{
  69. NSLog (@ "Insert failed");
  70. //        }
  71. //
  72. //    }];
  73. [Queue close];
  74. }
  75. To display data from all databases
  76. -(Ibaction) SHOWBTN: (ID) Sender {
  77. fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
  78. if (![ Database Open]) {
  79. Return
  80. }
  81. Note that the fmresultset here can not be closed, because Fmresultset automatically shuts down when the database is closed
  82. fmresultset* resultset=[database executequery:@ "select* from user"];
  83. nsstring* [email protected] "";
  84. while ([ResultSet next]) {
  85. nsstring* name=[resultset stringforcolumn:@ "name"];
  86. Nsinteger age=[resultset intforcolumn:@ "age"];
  87. nsstring* sex=[resultset stringforcolumn:@ "Sex"];
  88. Str=[str stringbyappendingformat:@ "name:%@,age:%d,sex:%@\n", Name,age,sex];
  89. NSLog (@ "name:%@,age:%d,sex:%@\n", name,age,sex);
  90. }
  91. _SHOWLABEL.TEXT=STR;
  92. [Database Close];
  93. }
  94. Inquire
  95. -(Ibaction) CHECKBTN: (ID) Sender {
  96. fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
  97. if (![ Database Open]) {
  98. Return
  99. }
  100. fmresultset* resultset=[database executequery:@ "select* from user where name =?", @ "Chen"];
  101. nsstring* [email protected] "";
  102. while ([ResultSet next]) {
  103. nsstring* name=[resultset stringforcolumn:@ "name"];
  104. Nsinteger age=[resultset intforcolumn:@ "age"];
  105. nsstring* sex=[resultset stringforcolumn:@ "Sex"];
  106. Str=[str stringbyappendingformat:@ "name:%@,age:%d,sex:%@\n", Name,age,sex];
  107. }
  108. _SHOWLABEL.TEXT=STR;
  109. [Database Close];
  110. }
  111. Delete
  112. -(Ibaction) DELETEBTN: (ID) Sender {
  113. fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
  114. if (![ Database Open]) {
  115. Return
  116. }
  117. BOOL delete=[database executeupdate:@ "Delete from user where name =?", @ "Chen"];
  118. if (delete) {
  119. uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "Delete succeeded" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
  120. [Alert show];
  121. [Alert release];
  122. }
  123. [Database Close];
  124. }
  125. Update
  126. -(Ibaction) UPDATEBTN: (ID) Sender {
  127. fmdatabase* database=[fmdatabase databasewithpath:[self DatabasePath];
  128. if (![ Database Open]) {
  129. Return
  130. }
  131. BOOL update=[database executeupdate:@ "Update user set age = where name=?", [NSNumber numberwithint:20],@ "Chen"];
  132. if (update) {
  133. uialertview* Alert=[[uialertview alloc] initwithtitle:@ "Prompt" message:@ "update succeeded" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
  134. [Alert show];
  135. [Alert release];
  136. }
  137. [Database Close];
  138. }
  139. -(nsstring*) DatabasePath
  140. {
  141. nsstring* path=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) objectAtIndex:0];
  142. nsstring* dbpath=[path stringbyappendingpathcomponent:@ "user.db"];
  143. return dbPath;
  144. }
  145. @end

iOS data storage mode Fmdb

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.