Parsing IOS development and learning database documentation-IOS database Encapsulation

Source: Internet
Author: User

IOS developmentLearning database documentation for IOSDatabaseEncapsulation is the content to be introduced in this article.IOS developmentFor more information about database encapsulation, see this article. There is not much content, mainly implemented by code.

H file

 
 
  1. # Import <Foundation/Foundation. h>
  2. # Import "sqlite3.h"
  3. @ Interface DatabaseOperation: NSObject {
  4. Sqlite3 * m_ SQL;
  5. NSString * m_dbName;
  6. }
  7. @ Property (nonatomic) sqlite3 * m_ SQL;
  8. @ Property (nonatomic, retain) NSString * m_dbName;
  9. -(Id) initWithDbName :( NSString *) dbname;
  10. -(BOOL) openOrCreateDatabase :( NSString *) DbName;
  11. -(BOOL) createTable :( NSString *) sqlCreateTable;
  12. -(Void) closeDatabase;
  13. -(BOOL) InsertTable :( NSString *) sqlInsert;
  14. -(BOOL) UpdataTable :( NSString *) sqlUpdata;
  15. -(NSArray *) querryTable :( NSString *) sqlQuerry;
  16. -(NSArray *) querryTableByCallBack :( NSString *) sqlQuerry;
  17. @ End
  18.  
  19. M file
  20.  
  21. # Import "DatabaseOperation. h"
  22. @ Implementation DatabaseOperation
  23. @ Synthesize m_ SQL;
  24. @ Synthesize m_dbName;
  25. -(Id) initWithDbName :( NSString *) dbname
  26. {
  27. Self = [super init];
  28. If (self! = Nil ){
  29. If ([self openOrCreateDatabase: dbname]) {
  30. [Self closeDatabase];
  31. }
  32. }
  33. Return self;
  34. }
  35. -(Id) init
  36. {
  37. NSAssert (0, @ "Never Use this. Please Call Use initWithDbName :( NSString *)");
  38. Return nil;
  39. }
  40. -(Void) dealloc
  41. {
  42. Self. m_ SQL = nil;
  43. Self. m_dbName = nil;
  44. [Super dealloc];
  45. }
  46. // Create a database
  47.  
  48. -(BOOL) openOrCreateDatabase :( NSString *) dbName
  49. {
  50. Self. m_dbName = dbName;
  51. NSArray * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  52. NSString * documentsDirectory = [path objectAtIndex: 0];
  53. If (sqlite3_open ([[documentsDirectorystringByAppendingPathComponent: dbName] UTF8String], & m_ SQL )! = SQLITE_ OK)
  54. {
  55. NSLog (@ "database creation failed ");
  56. Return NO;
  57. }
  58. Return YES;
  59. }
  60.  
  61. // Create a table
  62.  
  63. -(BOOL) createTable :( NSString *) sqlCreateTable
  64. {
  65. If (! [SelfopenOrCreateDatabase: self. m_dbName]) {
  66. Return NO;
  67. }
  68. Char * errorMsg;
  69. If (sqlite3_exec (self. m_ SQL, [sqlCreateTable UTF8String], NULL, NULL, & errorMsg )! = SQLITE_ OK)
  70. {
  71. NSLog (@ "failed to create data table: % s", errorMsg );
  72. Return NO;
  73. }
  74. [SelfcloseDatabase];
  75. Return YES;
  76. }
  77. // Close the database
  78. -(Void) closeDatabase
  79. {
  80. Sqlite3_close (self. m_ SQL );
  81. }
  82.  
  83. // Insert
  84.  
  85. -(BOOL) InsertTable :( NSString *) sqlInsert
  86. {
  87. If (! [SelfopenOrCreateDatabase: self. m_dbName]) {
  88. Return NO;
  89. }
  90. Char * errorMsg = NULL;
  91. If (sqlite3_exec (self. m_ SQL, [sqlInsertUTF8String], 0, NULL, & errorMsg) = SQLITE_ OK)
  92. {[SelfcloseDatabase];
  93. ReturnYES ;}
  94. Else {
  95. Printf ("failed to update table: % s", errorMsg );
  96. [SelfcloseDatabase];
  97. Return NO;
  98. }
  99. Return YES;
  100. }
  101. // Updata
  102.  
  103. -(BOOL) UpdataTable :( NSString *) sqlUpdata {
  104. If (! [SelfopenOrCreateDatabase: self. m_dbName]) {
  105. Return NO;
  106. }
  107. Char * errorMsg;
  108. If (sqlite3_exec (self. m_ SQL, [sqlUpdata UTF8String], 0, NULL, & errorMsg )! = SQLITE_ OK)
  109. {
  110. [SelfcloseDatabase];
  111. ReturnYES;
  112. } Else {
  113. ReturnNO;
  114. }
  115.  
  116. Return YES;
  117. }
  118. // Select
  119. -(NSArray *) querryTable :( NSString *) sqlQuerry
  120. {
  121. If (! [SelfopenOrCreateDatabase: self. m_dbName]) {
  122. Return nil;
  123. }
  124. Int row = 0;
  125. Int column = 0;
  126. Char * errorMsg = NULL;
  127. Char ** dbResult = NULL;
  128. NSMutableArray * array = [[NSMutableArrayalloc] init];
  129. If (sqlite3_get_table (m_ SQL, [sqlQuerryUTF8String], & dbResult, & row, & column, & errorMsg) = SQLITE_ OK)
  130. {
  131. If (0 = row ){
  132. [Self closeDatabase];
  133. Return nil;
  134. }
  135. Int index = column;
  136. For (int I = 0; I <row; I ++ ){
  137. NSMutableDictionary * dic = [[NSMutableDictionaryalloc] init];
  138. For (int j = 0; j <column; j ++ ){
  139. If (dbResult [index]) {
  140. NSString * value = [[NSStringalloc] initwithuf8string: dbResult [index];
  141. NSString * key = [[NSStringalloc] initwithuf8string: dbResult [j];
  142. [Dic setObject: value forKey: key];
  143. [Value release];
  144. [Key release];
  145. }
  146. Index ++;
  147. }
  148. [Array addObject: dic];
  149. [Dic release];
  150. }
  151. } Else {
  152. Printf ("% s", errorMsg );
  153. [SelfcloseDatabase];
  154. Return nil;
  155. }
  156. [SelfcloseDatabase];
  157. Return [array autorelease];
  158. }
  159. // Select
  160.  
  161. Int processData (void * arrayResult, int columnCount, char ** columnValue, char ** columnName)
  162. {
  163. Int I;
  164. NSMutableDictionary * dic = [[NSMutableDictionaryalloc] init];
  165. For (I = 0; I <columnCount; I ++)
  166. {
  167. If (columnValue [I]) {
  168. NSString * key = [[NSStringalloc] initwithuf8string: columnName [I];
  169. NSString * value = [[NSStringalloc] initwithuf8string: columnValue [I];
  170. [Dic setObject: value forKey: key];
  171. }
  172. }
  173. [(NSMutableArray *) arrayResult addObject: dic];
  174. [Dic release];
  175. Return 0;
  176. }
  177.  
  178. // Select
  179.  
  180. -(NSArray *) querryTableByCallBack :( NSString *) sqlQuerry
  181. {
  182. If (! [SelfopenOrCreateDatabase: self. m_dbName]) {
  183. Return nil;
  184. }
  185. Char * errorMsg = NULL;
  186. NSMutableArray * arrayResult = [[NSMutableArrayalloc] init];
  187. If (sqlite3_exec (self. m_ SQL, [sqlQuerryUTF8String], processData, (void *) arrayResult, & errorMsg )! = SQLITE_ OK ){
  188. Printf ("query error: % s", errorMsg );
  189. }
  190. [SelfcloseDatabase];
  191. Return [arrayResult autorelease];
  192. }
  193. @ End

Summary: AnalysisIOS developmentLearningDatabaseDocument for IOSDatabaseThe encapsulated content has been introduced. I hope this article will help you!

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.