iOS FMDB的使用(增,删,改,查,sqlite存取图片)


在上一篇博客我对sqlite的基本使用进行了详细介绍...

但是在实际开发中原生使用的频率是很少的...

这篇博客我将会较全面的介绍FMDB的使用...

例: 增,删,改,查,sqlite存取图片

有关框架的导入我在上一篇博客进行了详细介绍这里就不在介绍,没有看到上一篇博客的可以点击下面的连接.

iOS sqlite3 的基本使用(增 删 改 查)

接下来我会按照上篇博客的顺序,模式进行介绍.

(增删改查与sqlite存取图片我会通过两个工程介绍)

创建数据库与表

代码:

  1. @interface ViewController ()
  2. @property (nonnull, strong) FMDatabase * database;
  3. @end
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. //数据库在沙盒中的路径
  4. NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
  5. NSLog(@"%@",fileName);
  6. //创建数据库
  7. self.database = [[FMDatabase alloc]initWithPath:fileName];
  8. //打开数据库
  9. if ([self.database open]) {
  10. NSLog(@"打开数据库成功");
  11. //创建表 返回值为BOOL
  12. BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,name text)"];
  13. if (flag) {
  14. NSLog(@"成功建表");
  15. }else{
  16. NSLog(@"失败建表");
  17. }
  18. //关闭数据库
  19. [self.database close];
  20. }else{
  21. NSLog(@"打开数据库失败");
  22. }
  23. }

图片(创建数据库与表)

代码:

  1. - (IBAction)insert:(id)sender {
  2. if ([self.database open]) {
  3. for (NSInteger i = 0; i < 100; i ++) {
  4. BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (name) values(?)",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99)]];
  5. if (flag) {
  6. NSLog(@"插入成功");
  7. }else{
  8. NSLog(@"插入失败");
  9. }
  10. }
  11. }
  12. [self.database close];
  13. }

图片(增)

代码:

  1. - (IBAction)delete:(id)sender {
  2. if ([self.database open]) {
  3. for (NSInteger i = 0; i < 100; i ++) {
  4. BOOL flag = [self.database executeUpdate:@"delete from t_testOfFMDB where id < 50"];
  5. if (flag) {
  6. NSLog(@"删除成功");
  7. }else{
  8. NSLog(@"删除失败");
  9. }
  10. }
  11. }
  12. [self.database close];
  13. }

图片(删)

代码:

  1. - (IBAction)update:(id)sender {
  2. if ([self.database open]) {
  3. for (NSInteger i = 0; i < 100; i ++) {
  4. BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];
  5. if (flag) {
  6. NSLog(@"修改成功");
  7. }else{
  8. NSLog(@"修改失败");
  9. }
  10. }
  11. }
  12. [self.database close];
  13. }

图片(改)

代码:

  1. - (IBAction)select:(id)sender {
  2. if ([self.database open]) {
  3. //返回查询数据的结果集
  4. FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
  5. //查询表中的每一个记录
  6. while ([resultSet next]) {
  7. NSString * name = [resultSet stringForColumn:@"name"];
  8. NSLog(@"%@",name);
  9. }
  10. }
  11. [self.database close];
  12. }

图片(查)

整体代码

  1. //
  2. // ViewController.m
  3. // FMDB的使用
  4. //
  5. // Created by ma c on 16/5/10.
  6. // Copyright © 2016年 xubaoaichiyu. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "FMDB.h"
  10. @interface ViewController ()
  11. @property (nonnull, strong) FMDatabase * database;
  12. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  13. @end
  14. @implementation ViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. //数据库在沙盒中的路径
  18. NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
  19. NSLog(@"%@",fileName);
  20. //创建数据库
  21. self.database = [[FMDatabase alloc]initWithPath:fileName];
  22. //打开数据库
  23. if ([self.database open]) {
  24. NSLog(@"打开数据库成功");
  25. //创建表 返回值为BOOL
  26. BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,name text)"];
  27. if (flag) {
  28. NSLog(@"成功建表");
  29. }else{
  30. NSLog(@"失败建表");
  31. }
  32. //关闭数据库
  33. [self.database close];
  34. }else{
  35. NSLog(@"打开数据库失败");
  36. }
  37. }
  38. - (IBAction)insert:(id)sender {
  39. if ([self.database open]) {
  40. for (NSInteger i = 0; i < 100; i ++) {
  41. BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (name) values(?)",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99)]];
  42. if (flag) {
  43. NSLog(@"插入成功");
  44. }else{
  45. NSLog(@"插入失败");
  46. }
  47. }
  48. }
  49. [self.database close];
  50. }
  51. - (IBAction)update:(id)sender {
  52. if ([self.database open]) {
  53. for (NSInteger i = 0; i < 100; i ++) {
  54. BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];
  55. if (flag) {
  56. NSLog(@"修改成功");
  57. }else{
  58. NSLog(@"修改失败");
  59. }
  60. }
  61. }
  62. [self.database close];
  63. }
  64. - (IBAction)update:(id)sender {
  65. if ([self.database open]) {
  66. for (NSInteger i = 0; i < 100; i ++) {
  67. BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];
  68. if (flag) {
  69. NSLog(@"修改成功");
  70. }else{
  71. NSLog(@"修改失败");
  72. }
  73. }
  74. }
  75. [self.database close];
  76. }
  77. - (IBAction)select:(id)sender {
  78. if ([self.database open]) {
  79. //返回查询数据的结果集
  80. FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
  81. //查询表中的每一个记录
  82. while ([resultSet next]) {
  83. NSString * name = [resultSet stringForColumn:@"name"];
  84. NSLog(@"%@",name);
  85. }
  86. }
  87. [self.database close];
  88. }
  89. @end

sqlite存取图片

这里有几点注意:

  • 图片无法直接存入数据库中需要把图片转换为二进制后存入数据库
  • 二进制存储的格式为 blob

整体代码

  1. //
  2. // ViewController.m
  3. // FMDB的使用
  4. //
  5. // Created by ma c on 16/5/10.
  6. // Copyright © 2016年 xubaoaichiyu. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "FMDB.h"
  10. @interface ViewController ()
  11. @property (nonnull, strong) FMDatabase * database;
  12. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  13. @end
  14. @implementation ViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. //数据库在沙盒中的路径
  18. NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
  19. NSLog(@"%@",fileName);
  20. //创建数据库
  21. self.database = [[FMDatabase alloc]initWithPath:fileName];
  22. //打开数据库
  23. if ([self.database open]) {
  24. NSLog(@"打开数据库成功");
  25. //创建表 返回值为BOOL
  26. BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,image blob)"];
  27. if (flag) {
  28. NSLog(@"成功建表");
  29. }else{
  30. NSLog(@"失败建表");
  31. }
  32. //关闭数据库
  33. [self.database close];
  34. }else{
  35. NSLog(@"打开数据库失败");
  36. }
  37. }
  38. - (IBAction)insert:(id)sender {
  39. if ([self.database open]) {
  40. NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@"BG.jpg"], 1);
  41. BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (image) values(?)",data];
  42. if (flag) {
  43. NSLog(@"插入成功");
  44. }else{
  45. NSLog(@"插入失败");
  46. }
  47. }
  48. [self.database close];
  49. }
  50. - (IBAction)delete:(id)sender {
  51. if ([self.database open]) {
  52. BOOL flag = [self.database executeUpdate:@"delete from t_testOfFMDB"];
  53. if (flag) {
  54. NSLog(@"删除成功");
  55. }else{
  56. NSLog(@"删除失败");
  57. }
  58. }
  59. [self.database close];
  60. }
  61. - (IBAction)update:(id)sender {
  62. if ([self.database open]) {
  63. NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@"CX.jpg"], 1);
  64. BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set image = ?",data];
  65. if (flag) {
  66. NSLog(@"修改成功");
  67. }else{
  68. NSLog(@"修改失败");
  69. }
  70. }
  71. [self.database close];
  72. }
  73. - (IBAction)select:(id)sender {
  74. if ([self.database open]) {
  75. //返回查询数据的结果集
  76. FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
  77. //查询表中的每一个记录
  78. while ([resultSet next]) {
  79. NSData * data = [resultSet dataForColumn:@"image"];
  80. UIImage * image = [UIImage imageWithData:data];
  81. self.imageView.image = image;
  82. }
  83. }
  84. [self.database close];
  85. }
  86. @end

测试结果

图片(插入后查询)

图片(修改后查询)

图片(删除后查询 由于显示无法证实 这里查看数据库)

iOS FMDB的使用(增,删,改,查,sqlite存取图片)的更多相关文章

  1. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  2. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  3. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  4. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  5. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  6. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

  7. 简单的php数据库操作类代码(增,删,改,查)

    这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...

  8. MongoDB增 删 改 查

    增 增加单篇文档 > db.stu.insert({sn:'001', name:'lisi'}) WriteResult({ "nInserted" : 1 }) > ...

  9. Go语言之进阶篇mysql增 删 改 查

    一.mysql操作基本语法 1.创建名称nulige的数据库 CREATE DATABASE nulige DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ...

随机推荐

  1. 交换排序---冒泡排序算法(Javascript版)

    比较相邻的元素.如果第一个比第二个大,就交换他们两个.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数.针对所有的元素重复以上的步骤,除了最后一个.持续 ...

  2. jQuery Ajax上传文件

    JS代码: //保存 function btnAdd() { var formData = new FormData($("#frm")[0]); $.ajax({ url: &q ...

  3. spring笔记1 spring MVC的基础知识1

    1,spring MVC的流程 优秀的展现层框架-Spring MVC,它最出彩的地方是注解驱动和支持REST风格的url.   流程编号 完成的主要任务 补充 1 用户访问web页面,发送一个htt ...

  4. Cygwin的安装

    Android开发要用到NDK,装了一个虚拟机,老是不行. 后来安装了一个cygwin,安装完毕后unset home,再export NDK,就可以使用了,非常方便,不用像虚拟机那样经常切换.

  5. Java 经典实例: Unicode字符和String之间的转换

    在Java诞生之际,Unicode码是一个16位的字符集,因此char值似乎顺其自然为16位宽,多年来一个char变量几乎可以表示任何Unicode字符. /** * Created by Frank ...

  6. 网上图书商城2--Category模块

    sql CREATE TABLE `t_category` ( `cid` char(32) NOT NULL, `cname` varchar(50) DEFAULT NULL, `pid` cha ...

  7. fakeLoader页面加载前loading演示8种效果

    提高用户体验的插件fakeLoader页面加载前loading演示8种效果 在线预览 下载地址 示例代码 <div id="main"> <div class=& ...

  8. CSS3中DIV水平垂直居中-2(3)

    用到CSS3中display的新属性. HTML <div class="parent"> </div> CSS html,body{ width: 100 ...

  9. 【问题】js 改变鼠标样式,chrome浏览器不能立即更新,暂没有解决办法

    元素的css,cursor可以改变鼠标样式.也就是鼠标放到元素上去时,改变为相应状态. 通过JS改变cursor时,我发现chrome浏览器不能立即更新,需要动一下鼠标才行,试了几个其它浏览器都是立即 ...

  10. 浅析字符串操作方法slice、substr、substring及其IE兼容性

    在截取字符串时常常会用到substr().substring().slice()方法,有时混淆之间的用法,故总结下.   slice() 定义:接受一个或者两个参数,第一个参数指定子字符串的开始位置. ...