sqlite1-1

前端之家收集整理的这篇文章主要介绍了sqlite1-1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //
  2. // ViewController.m
  3. // sqliteText
  4. //
  5. // Created by jerehedu on 15/2/2.
  6. // Copyright (c) 2015年 baidu. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import <sqlite3.h> //1. 导入sqlite3头文件
  11.  
  12. @interface ViewController ()
  13.  
  14. @end
  15.  
  16. @implementation ViewController
  17. {
  18. sqlite3 *db;//2. 声明sqlite3对象
  19. }
  20.  
  21.  
  22.  
  23. - (NSString *)getUserDocumentPath
  24. {
  25. //3. 转换沙盒路径为C字符串
  26. NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  27. NSString *documentPath=[path lastObject];
  28. return documentPath;
  29. }
  30.  
  31. - (NSString *)appendingPathComponent:(NSString *)documentPath andFileName:(NSString*)fileName
  32. {
  33. NSString *dbPath = [documentPath stringByAppendingPathComponent:fileName];
  34. return dbPath;
  35. }
  36. - (BOOL)openOrCreatesqliteDBWithDBPath:(NSString*)dbPath
  37. {
  38. const char *p = [dbPath UTF8String];
  39. //4. 打开或创建数据库
  40. int res = sqlite3_open(p,&db);
  41. if (res == sqlITE_OK) {
  42. return YES;
  43. }
  44. return NO;
  45. }
  46. #pragma mark
  47. - (BOOL)execsqlNoQueryWith:(NSString*)sql
  48. {
  49. int insert_res = sqlite3_exec(db,[sql UTF8String],NULL,NULL);
  50. if (insert_res == sqlITE_OK) {
  51. return YES;
  52. }
  53. return NO;
  54. }
  55. // <1>.......
  56. - (sqlite3_stmt*)execQueryWithsql:(NSString*)sql
  57. {
  58. sqlite3_stmt *stmt;
  59. int pre_res = sqlite3_prepare(db,-1,&stmt,NULL);
  60. if (pre_res == YES) {
  61. return stmt;
  62. }
  63. return NULL;
  64. }
  65. // <2>.......
  66. - (sqlite3_stmt*)execQueryWithsql:(NSString*)sql andWithParams:(NSArray*)params
  67. {
  68. sqlite3_stmt *stmt;
  69. int pre_res = sqlite3_prepare_v2(db,NULL);
  70. if (pre_res == sqlITE_OK) {
  71. //绑定参数
  72. //先判断是否参数为nil,然后循环绑定多个参数
  73. if (params != nil)
  74. {
  75. for (int i = 0; i <[params count]; i++)
  76. {
  77. id obj = params[i];
  78. if (obj == nil) {
  79. sqlite3_bind_null(stmt,i+1);
  80. }
  81. else if ([obj respondsToSelector:@selector(objCType)]){
  82. //respondsToSelector判断是否有objCType方法,返回BOLL型
  83. if (strstr("ilsILS",[obj objCType] )){
  84. //strstr (char *,char*)判断是否在char*中出现过
  85. sqlite3_bind_int(stmt,i+1,[obj intValue]);
  86. }
  87. else if (strstr("fdFD",[obj objCType])){
  88. sqlite3_bind_double(stmt,[obj doubleValue]);
  89. }
  90. else{
  91. stmt = nil;
  92. }
  93. }
  94. else if([obj respondsToSelector:@selector(UTF8String)]){
  95. sqlite3_bind_text(stmt,[obj UTF8String],NULL);
  96. }else{
  97. stmt = nil;
  98. }
  99. }
  100. return stmt;
  101. }
  102. }
  103. return NULL;
  104. }
  105.  
  106. - (void)viewDidLoad
  107. {
  108. [super viewDidLoad];
  109. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,320,568)];
  110. label.font = [UIFont systemFontOfSize:18];
  111. [label setNumberOfLines:0];
  112. label.textColor = [UIColor redColor];
  113. [self.view addSubview:label];
  114. // sqlite3 *db;//2. 声明sqlite3对象
  115.  
  116. NSString *documentPath;
  117. documentPath = [self getUserDocumentPath];
  118. NSString *dbPath;
  119. dbPath = [self appendingPathComponent:documentPath andFileName:@"demo.sqlite"];
  120. NSLog(@"%@",dbPath);
  121. //5. 判断是否打开成功
  122. if ([self openOrCreatesqliteDBWithDBPath:dbPath]) {
  123. NSLog(@"db is opened");
  124. //构造sql语句
  125. NSString *sql = @"create table if not exists tempYear(t_id integer primary key autoincrement,t_name varchar(20) not null,t_date date)";
  126. /*
  127. 第一个参数: sqlite3 对象
  128. 第二个参数: sql语句
  129. 第三个参数: 回调函数
  130. 第四个函数: 回调函数的参数
  131. 第五个参数: 错误信息
  132. */
  133. int exec_res = sqlite3_exec(db,NULL);
  134. if (exec_res == sqlITE_OK) {
  135. NSLog(@"table is created");
  136. }
  137. //增加
  138. NSString *insert_sql = @"insert into tempYear(t_name) values('jeack')";
  139. if ([self execsqlNoQueryWith:insert_sql]) {
  140. NSLog(@"one recorder is inserted");
  141. }
  142. //删除
  143. NSString *delete_sql = @"delete from tempYear where t_id=2";
  144. if ([self execsqlNoQueryWith:delete_sql]) {
  145. NSLog(@"delete id already done");
  146. }
  147. //更改
  148. NSString *update_sql = @"update tempYear set t_name ='title' where t_id=4 " ;
  149. if ([self execsqlNoQueryWith:update_sql]) {
  150. NSLog(@"update id already done");
  151. }
  152.  
  153. //查询
  154. NSString *search_sql = @"select * from tempYear where t_id>? and t_name like ?";//占位符
  155. int search_t_id=4;
  156. NSString *search_t_name = @"j%";
  157. //select * from user where username='admin' and pwd ='admin';
  158. sqlite3_stmt *stmt;
  159. /*>>>准备执行查询sql语句
  160. 第一个参数:sqlite对象
  161. 第二个参数:执行的sql语句
  162. 第三个参数:sql语句的长度,通常用-1来代表(系统自己计算长度)也可以用strlen([search_sql UTF8String])函数来计算
  163. 第四个参数:sqlite3_stmt对象
  164. 第五个参数:未执行的sql语句
  165. */
  166. // int prepare_res = sqlite3_prepare_v2(db,[search_sql UTF8String],NULL);
  167. // if (prepare_res == sqlITE_OK) {
  168. /*
  169. sqlite3_bind_int(stmt,1,search_t_id);
  170. 绑定参数
  171. 第一个参数:sqlite_stmt对象
  172. 第二个参数:占位符索引,从1开始
  173. 第三个参数:替代占位符的真实参数
  174. */
  175.  
  176. // sqlite3_bind_int(stmt,search_t_id);
  177. // sqlite3_bind_text(stmt,2,[search_t_name UTF8String],NULL);
  178. NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:search_t_id],search_t_name,nil];
  179. stmt = [self execQueryWithsql:search_sql andWithParams:array];
  180. while (sqlite3_step(stmt)==sqlITE_ROW) {
  181. int t_id = sqlite3_column_int(stmt,0);
  182. const unsigned char *t_name = sqlite3_column_text(stmt,1);
  183. NSLog(@"id=%d,name=%s",t_id,t_name);
  184. }
  185. //释放stmt
  186. sqlite3_finalize(stmt);
  187. }
  188. //关闭数据库
  189. sqlite3_close(db);
  190. }
  191.  
  192. - (void)didReceiveMemoryWarning
  193. {
  194. [super didReceiveMemoryWarning];
  195. // Dispose of any resources that can be recreated.
  196. }
  197.  
  198. @end

猜你在找的Sqlite相关文章