sqlite1-2增 删 改 查方法的简单封装

前端之家收集整理的这篇文章主要介绍了sqlite1-2增 删 改 查方法的简单封装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. // sqliteHelper.m
  2. // sqliteText3
  3. //
  4. // Created by jerehedu on 15/2/3.
  5. // Copyright (c) 2015年 baidu. All rights reserved.
  6. //
  7.  
  8. #import "sqliteHelper.h"
  9. #import <sqlite3.h>
  10.  
  11.  
  12. #define DBNAME @"demo.sqlite"
  13.  
  14. @implementation sqliteHelper
  15. {
  16. sqlite3 *db;//2. 声明sqlite3对象
  17. }
  18.  
  19. - (NSString *)getUserDocumentPath
  20. {
  21. //3. 转换沙盒路径为C字符串
  22. NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  23. NSString *documentPath=[path lastObject];
  24. return documentPath;
  25. }
  26.  
  27. - (NSString *)appendingPathComponent:(NSString *)documentPath andFileName:(NSString*)fileName
  28. {
  29. NSString *dbPath = [documentPath stringByAppendingPathComponent:fileName];
  30. return dbPath;
  31. }
  32. - (BOOL)openOrCreatesqliteDBWithDBPathWithFileName:(NSString*)fileName
  33. {
  34. NSString *documentPath = [self getUserDocumentPath];
  35. NSString *sqlitePath = [self appendingPathComponent:documentPath andFileName:fileName];
  36.  
  37. const char *p = [sqlitePath UTF8String];
  38. //4. 打开或创建数据库
  39. int res = sqlite3_open(p,&db);
  40. if (res == sqlITE_OK) {
  41. return YES;
  42. }
  43. return NO;
  44. }
  45. #pragma mark
  46. - (BOOL)execsqlNoQueryWith:(NSString*)sql
  47. {
  48. [self openOrCreatesqliteDBWithDBPathWithFileName:DBNAME];
  49. BOOL isExecSuccess = NO;
  50. int insert_res = sqlite3_exec(db,[sql UTF8String],NULL,NULL);
  51. if (insert_res == sqlITE_OK) {
  52. isExecSuccess =YES;
  53. }
  54. sqlite3_close(db);
  55. return isExecSuccess;
  56. }
  57.  
  58. - (BOOL)execsqlNoQueryWith:(NSString*)sql andWithParams:(NSArray*)params
  59. {
  60. BOOL isExecSuccess = NO;
  61. sqlite3_stmt *stmt = [self prepareStatementWithsql:sql andWithParams:params];
  62. if (sqlite3_step(stmt)==sqlITE_DONE) {
  63. isExecSuccess = YES;
  64. }
  65. sqlite3_finalize(stmt);
  66. sqlite3_close(db);
  67. return isExecSuccess;
  68. }
  69.  
  70. // <2>.......
  71. - (sqlite3_stmt*)prepareStatementWithsql:(NSString*)sql andWithParams:(NSArray*)params
  72. {
  73. [self openOrCreatesqliteDBWithDBPathWithFileName:DBNAME];
  74. sqlite3_stmt *stmt;
  75. int pre_res = sqlite3_prepare_v2(db,-1,&stmt,NULL);
  76. if (pre_res == sqlITE_OK) {
  77. //绑定参数
  78. //先判断是否参数为nil,然后循环绑定多个参数
  79. if (params != nil)
  80. {
  81. for (int i = 0; i <[params count]; i++)
  82. {
  83. id obj = params[i];
  84. if (obj == nil) {
  85. sqlite3_bind_null(stmt,i+1);
  86. }
  87. else if ([obj respondsToSelector:@selector(objCType)]){
  88. //respondsToSelector判断是否有objCType方法,返回BOLL型
  89. if (strstr("ilsILS",[obj objCType] )){
  90. //strstr (char *,char*)判断是否在char*中出现过
  91. sqlite3_bind_int(stmt,i+1,[obj intValue]);
  92. }
  93. else if (strstr("fdFD",[obj objCType])){
  94. sqlite3_bind_double(stmt,[obj doubleValue]);
  95. }
  96. else{
  97. stmt = nil;
  98. }
  99. }
  100. else if([obj respondsToSelector:@selector(UTF8String)]){
  101. sqlite3_bind_text(stmt,[obj UTF8String],NULL);
  102. }else{
  103. stmt = nil;
  104. }
  105. }
  106. }
  107. return stmt;
  108. }
  109. return NULL;
  110. }
  111.  
  112. - (void)closeDB{
  113. if (db) {
  114. sqlite3_close(db);
  115. }
  116. }
  117.  
  118. @end

猜你在找的Sqlite相关文章