SQLite数据库类

前端之家收集整理的这篇文章主要介绍了SQLite数据库类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

封装好的sqlite数据库
首先你先导入静态库文件 libsqlite3.dylib

  1. .h声明方法
  2. #pragma mark - 单例
  3.  
  4.  
  5.  
  6. //创建单例方法 注意:是类方法
  7.  
  8. +(instancetype)shareDataBaseHandle;
  9.  
  10.  
  11.  
  12. #pragma mark - 数据库相关操作
  13.  
  14. //打开数据库
  15.  
  16. -(void)open;
  17.  
  18.  
  19.  
  20. //创建表单
  21.  
  22. -(void)createTable;
  23.  
  24.  
  25.  
  26. //插入数据(学生信息)
  27.  
  28. -(void)insertStudent:(Student *)student;
  29.  
  30.  
  31.  
  32. //更新学生信息
  33.  
  34. -(void)updataStudent:(Student *)student number:(NSInteger)number;
  35.  
  36.  
  37.  
  38. //删除数据
  39.  
  40. -(void)deleteStudent:(NSInteger)number;
  41.  
  42.  
  43.  
  44. //删除表格
  45.  
  46. -(void)dropTable;
  47.  
  48.  
  49.  
  50. //关闭数据库
  51.  
  52. -(void)close;
  53.  
  54.  
  55.  
  56. //查询
  57.  
  58. -(NSArray *)selectStudentWithSex:(NSString *)sex;
  59.  
  60.  
  61. .m写入方法
  62. //创建一个数据库对象
  63.  
  64. static sqlite3 *db;
  65.  
  66.  
  67.  
  68. +(instancetype)shareDataBaseHandle
  69.  
  70. {
  71.  
  72. // static 修饰词作用:1 在静态区开空间,程序退出时,内存才释放 2 只初始化一次
  73.  
  74. static DataBaseHandle *databaseHandle = nil;
  75.  
  76.  
  77.  
  78. if (databaseHandle == nil) {
  79.  
  80. databaseHandle = [[DataBaseHandle alloc] init];
  81.  
  82. }
  83.  
  84.  
  85.  
  86. return databaseHandle;
  87.  
  88. }
  89.  
  90.  
  91.  
  92. //打开数据库
  93.  
  94. -(void)open
  95.  
  96. {
  97.  
  98. NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];
  99.  
  100. NSString *sqlPath = [docPath stringByAppendingPathComponent:@"students.sqlite"];
  101.  
  102.  
  103.  
  104. //如果数据库已经打开,直接返回,不运行下面代码
  105.  
  106. if (db != nil) {
  107.  
  108. NSLog(@"数据库已经打开");
  109.  
  110. return;
  111.  
  112. }
  113.  
  114.  
  115.  
  116. //核心API : sqlite3_open 函数
  117.  
  118. int result = sqlite3_open(sqlPath.UTF8String,&db);
  119.  
  120.  
  121.  
  122. //根据函数返回值,判断操作是否正确
  123.  
  124. if (result == sqlITE_OK) {
  125.  
  126. NSLog(@"数据库打开成功");
  127.  
  128. }else{
  129.  
  130. NSLog(@"数据库打开失败");
  131.  
  132. }
  133.  
  134. }
  135.  
  136.  
  137.  
  138. //创建表单
  139.  
  140. -(void)createTable
  141.  
  142. {
  143.  
  144. //创建sql语句
  145.  
  146. NSString *createsql = @"CREATE TABLE IF NOT EXISTS lanou0613(number integer PRIMARY KEY AUTOINCREMENT,name TEXT,sex TEXT,age integer)"; //lanou0613是表单名字可以更改
  147.  
  148.  
  149.  
  150. //核心API
  151.  
  152. int result = sqlite3_exec(db,createsql.UTF8String,NULL,nil);
  153.  
  154.  
  155.  
  156. if (sqlITE_OK == result) {
  157.  
  158. NSLog(@"创建表格成功");
  159.  
  160. }else{
  161.  
  162. NSLog(@"创建表格失败");
  163.  
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170. //插入数据(学生信息)
  171.  
  172. -(void)insertStudent:(Student *)student
  173.  
  174. {
  175.  
  176. //创建sql语句
  177.  
  178. NSString *insertsql = [NSString stringWithFormat:@"INSERT INTO lanou0613(name,sex,age) VALUES('%@','%@','%ld')",student.name,student.sex,student.age];
  179.  
  180.  
  181.  
  182. //核心API
  183.  
  184. int result = sqlite3_exec(db,insertsql.UTF8String,nil);
  185.  
  186. if (sqlITE_OK == result) {
  187.  
  188. NSLog(@"插入数据成功");
  189.  
  190. }else{
  191.  
  192. NSLog(@"插入数据失败");
  193.  
  194. }
  195.  
  196. }
  197.  
  198.  
  199.  
  200. //更新学生信息
  201.  
  202. -(void)updataStudent:(Student *)student number:(NSInteger)number
  203.  
  204. {
  205.  
  206. NSString *updatasql = [NSString stringWithFormat:@"UPDATE lanou0613 SET name = '%@',sex = '%@',age = '%ld' WHERE number = '%ld'",student.age,number];
  207.  
  208.  
  209.  
  210. int result = sqlite3_exec(db,updatasql.UTF8String,nil);
  211.  
  212.  
  213.  
  214. if (sqlITE_OK == result) {
  215.  
  216.  
  217.  
  218. NSLog(@"更新数据成功");
  219.  
  220. }else{
  221.  
  222. NSLog(@"更新数据失败");
  223.  
  224. }
  225.  
  226.  
  227.  
  228. }
  229.  
  230.  
  231.  
  232. //删除数据
  233.  
  234. -(void)deleteStudent:(NSInteger)number
  235.  
  236. {
  237.  
  238. NSString *deletesql = [NSString stringWithFormat:@"DELETE FROM lanou0613 WHERE number = '%ld'",number];
  239.  
  240. int result = sqlite3_exec(db,deletesql.UTF8String,nil);
  241.  
  242. if (sqlITE_OK == result) {
  243.  
  244. NSLog(@"删除成功");
  245.  
  246. }else{
  247.  
  248. NSLog(@"删除失败");
  249.  
  250. }
  251.  
  252.  
  253.  
  254. }
  255.  
  256.  
  257.  
  258. //删除表格
  259.  
  260. -(void)dropTable
  261.  
  262. {
  263.  
  264. NSString *dropTable = [NSString stringWithFormat:@"DROP TABLE lanou0613"];
  265.  
  266. int result = sqlite3_exec(db,dropTable.UTF8String,nil);
  267.  
  268. if (sqlITE_OK == result) {
  269.  
  270. NSLog(@"删除表格成功");
  271.  
  272. }else{
  273.  
  274. NSLog(@"删除表格失败");
  275.  
  276. }
  277.  
  278. }
  279.  
  280.  
  281.  
  282. //关闭数据库
  283.  
  284. -(void)close
  285.  
  286. {
  287.  
  288. int result = sqlite3_close(db);
  289.  
  290. if (sqlITE_OK == result) {
  291.  
  292. db = nil;
  293.  
  294. NSLog(@"关闭数据库成功");
  295.  
  296. }else{
  297.  
  298. NSLog(@"关闭数据库失败");
  299.  
  300. }
  301.  
  302. }
  303.  
  304.  
  305.  
  306. //查询
  307.  
  308. -(NSArray *)selectStudentWithSex:(NSString *)sex
  309.  
  310. {
  311.  
  312. //创建数组
  313.  
  314. NSMutableArray *arr = [NSMutableArray array];
  315.  
  316.  
  317.  
  318. //创建SQL查询语句
  319.  
  320. NSString *selectsql = [NSString stringWithFormat:@"SELECT *FROM lanou0613 WHERE sex = '%@'",sex];
  321.  
  322.  
  323.  
  324. //准备好的sql语句对象
  325.  
  326. sqlite3_stmt *stmt = nil;
  327.  
  328.  
  329.  
  330. //
  331.  
  332. int result = sqlite3_prepare_v2(db,selectsql.UTF8String,-1,&stmt,nil);
  333.  
  334. if (sqlITE_OK == result) {
  335.  
  336. NSLog(@"查询成功");
  337.  
  338.  
  339.  
  340. // //执行准备好的sql语句
  341.  
  342. // sqlite3_step(stmt);
  343.  
  344. //sqlite3_step(stmt); 这个函数的返回值为 sqlITE_ROW
  345.  
  346.  
  347.  
  348. //循环 判断到什么时候结束
  349.  
  350. while (sqlITE_ROW == sqlite3_step(stmt)) {
  351.  
  352.  
  353.  
  354. //但需要对象查询之后的结果进行处理操作时,使用sqlite3_column_ 函数
  355.  
  356. const unsigned char *name = sqlite3_column_text(stmt,1);//取出name
  357.  
  358. const unsigned char *sex = sqlite3_column_text(stmt,2);//取出sex
  359.  
  360. int age = sqlite3_column_int(stmt,3); //取出age
  361.  
  362.  
  363.  
  364. //创建Model对象,赋值
  365.  
  366. Student *stu = [[Student alloc] init];
  367.  
  368. stu.name = [NSString stringWithUTF8String:(const char *)name];
  369.  
  370. stu.sex = [NSString stringWithUTF8String:(const char *)sex];
  371.  
  372. stu.age = age;
  373.  
  374.  
  375.  
  376. //model 对象添加到数组中
  377.  
  378. [arr addObject:stu];
  379.  
  380. [stu release];
  381.  
  382. }
  383.  
  384.  
  385.  
  386.  
  387.  
  388. //销毁 准备好的sql语句对象
  389.  
  390. sqlite3_finalize(stmt);
  391.  
  392. }else{
  393.  
  394.  
  395.  
  396. NSLog(@"查询失败");
  397.  
  398.  
  399.  
  400. sqlite3_finalize(stmt);
  401.  
  402. }
  403.  
  404. return arr;
  405.  
  406.  
  407.  
  408. }

猜你在找的Sqlite相关文章