iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片)
在上一篇博客我对sqlite的基本使用进行了详细介绍...
但是在实际开发中原生使用的频率是很少的...
这篇博客我将会较全面的介绍FMDB的使用...
例: 增,删,改,查,sqlite存取图片
有关框架的导入我在上一篇博客进行了详细介绍这里就不在介绍,没有看到上一篇博客的可以点击下面的连接.
接下来我会按照上篇博客的顺序,模式进行介绍.
(增删改查与sqlite存取图片我会通过两个工程介绍)
创建数据库与表
代码:
@interface ViewController ()
@property (nonnull, strong) FMDatabase * database;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//数据库在沙盒中的路径
NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
NSLog(@"%@",fileName);
//创建数据库
self.database = [[FMDatabase alloc]initWithPath:fileName];
//打开数据库
if ([self.database open]) {
NSLog(@"打开数据库成功");
//创建表 返回值为BOOL
BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,name text)"];
if (flag) {
NSLog(@"成功建表");
}else{
NSLog(@"失败建表");
}
//关闭数据库
[self.database close];
}else{
NSLog(@"打开数据库失败");
}
}
图片(创建数据库与表)

增
代码:
- (IBAction)insert:(id)sender {
if ([self.database open]) {
for (NSInteger i = 0; i < 100; i ++) {
BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (name) values(?)",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99)]];
if (flag) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败");
}
}
}
[self.database close];
}
图片(增)

删
代码:
- (IBAction)delete:(id)sender {
if ([self.database open]) {
for (NSInteger i = 0; i < 100; i ++) {
BOOL flag = [self.database executeUpdate:@"delete from t_testOfFMDB where id < 50"];
if (flag) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
}
}
}
[self.database close];
}
图片(删)

改
代码:
- (IBAction)update:(id)sender {
if ([self.database open]) {
for (NSInteger i = 0; i < 100; i ++) {
BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];
if (flag) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
}
[self.database close];
}
图片(改)

查
代码:
- (IBAction)select:(id)sender {
if ([self.database open]) {
//返回查询数据的结果集
FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
//查询表中的每一个记录
while ([resultSet next]) {
NSString * name = [resultSet stringForColumn:@"name"];
NSLog(@"%@",name);
}
}
[self.database close];
}
图片(查)

整体代码
//
// ViewController.m
// FMDB的使用
//
// Created by ma c on 16/5/10.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
//
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property (nonnull, strong) FMDatabase * database;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//数据库在沙盒中的路径
NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
NSLog(@"%@",fileName);
//创建数据库
self.database = [[FMDatabase alloc]initWithPath:fileName];
//打开数据库
if ([self.database open]) {
NSLog(@"打开数据库成功");
//创建表 返回值为BOOL
BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,name text)"];
if (flag) {
NSLog(@"成功建表");
}else{
NSLog(@"失败建表");
}
//关闭数据库
[self.database close];
}else{
NSLog(@"打开数据库失败");
}
}
- (IBAction)insert:(id)sender {
if ([self.database open]) {
for (NSInteger i = 0; i < 100; i ++) {
BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (name) values(?)",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99)]];
if (flag) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败");
}
}
}
[self.database close];
}
- (IBAction)update:(id)sender {
if ([self.database open]) {
for (NSInteger i = 0; i < 100; i ++) {
BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];
if (flag) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
}
[self.database close];
}
- (IBAction)update:(id)sender {
if ([self.database open]) {
for (NSInteger i = 0; i < 100; i ++) {
BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];
if (flag) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
}
[self.database close];
}
- (IBAction)select:(id)sender {
if ([self.database open]) {
//返回查询数据的结果集
FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
//查询表中的每一个记录
while ([resultSet next]) {
NSString * name = [resultSet stringForColumn:@"name"];
NSLog(@"%@",name);
}
}
[self.database close];
}
@end
sqlite存取图片
这里有几点注意:
- 图片无法直接存入数据库中需要把图片转换为二进制后存入数据库
- 二进制存储的格式为 blob
整体代码
//
// ViewController.m
// FMDB的使用
//
// Created by ma c on 16/5/10.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
//
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property (nonnull, strong) FMDatabase * database;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//数据库在沙盒中的路径
NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
NSLog(@"%@",fileName);
//创建数据库
self.database = [[FMDatabase alloc]initWithPath:fileName];
//打开数据库
if ([self.database open]) {
NSLog(@"打开数据库成功");
//创建表 返回值为BOOL
BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,image blob)"];
if (flag) {
NSLog(@"成功建表");
}else{
NSLog(@"失败建表");
}
//关闭数据库
[self.database close];
}else{
NSLog(@"打开数据库失败");
}
}
- (IBAction)insert:(id)sender {
if ([self.database open]) {
NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@"BG.jpg"], 1);
BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (image) values(?)",data];
if (flag) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败");
}
}
[self.database close];
}
- (IBAction)delete:(id)sender {
if ([self.database open]) {
BOOL flag = [self.database executeUpdate:@"delete from t_testOfFMDB"];
if (flag) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
}
}
[self.database close];
}
- (IBAction)update:(id)sender {
if ([self.database open]) {
NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@"CX.jpg"], 1);
BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set image = ?",data];
if (flag) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
[self.database close];
}
- (IBAction)select:(id)sender {
if ([self.database open]) {
//返回查询数据的结果集
FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
//查询表中的每一个记录
while ([resultSet next]) {
NSData * data = [resultSet dataForColumn:@"image"];
UIImage * image = [UIImage imageWithData:data];
self.imageView.image = image;
}
}
[self.database close];
}
@end
测试结果
图片(插入后查询)

图片(修改后查询)

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

iOS FMDB的使用(增,删,改,查,sqlite存取图片)的更多相关文章
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- python基础中的四大天王-增-删-改-查
列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...
- 简单的php数据库操作类代码(增,删,改,查)
这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...
- MongoDB增 删 改 查
增 增加单篇文档 > db.stu.insert({sn:'001', name:'lisi'}) WriteResult({ "nInserted" : 1 }) > ...
- Go语言之进阶篇mysql增 删 改 查
一.mysql操作基本语法 1.创建名称nulige的数据库 CREATE DATABASE nulige DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ...
随机推荐
- [译]学习IPython进行交互式计算和数据可视化(二)
第一章:开始使用IPython 在本章中,我们首先进行一遍IPython的安装过程,在大概浏览一下IPython提供的功能.IPython提供了一个高度优化的Python控制台和Notebook.除此 ...
- MVVM-Sidekick 之SendToEventRouterAction使用
在WP开发中点击列表项跳转到详情页是一个很常用的功能,但是有可能项模板中还有其他的区域,比如点击标题跳转到详情页,点击"赞"图标送一个赞,点击"踩"图标踩一下, ...
- 数据库中char, varchar, nvarchar的差异
char char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符. nvarc ...
- javascript学习笔记1-document.write
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...
- android The connection to adb is down 错误信息
之前运行eclipse执行android应用都没问题,今天突然出问题了,控制台报错: The connection to adb is down, and a severe error has occ ...
- linux下的C语言开发
在很多人的眼里,C语言和linux常常是分不开的.这其中的原因很多,其中最重要的一部分我认为是linux本身就是C语言的杰出作品.当然,linux操作系统本身对C语言的支持也是相当到位的.作为一个真正 ...
- PHP表单数据写入MySQL代码
<h1>插入操作</h1> <?php if(!isset($_POST['submit'])){ //如果没有表单提交,显示一个表单 ?> <form ac ...
- jquery分隔Url的param方法
最近需要分隔url的querystring,用到了特意记录一下.方法: //获取url中的paramsvar search = location.search.substring(1);//param ...
- (转)Android 系统 root 破解原理分析
现在Android系统的root破解基本上成为大家的必备技能!网上也有很多中一键破解的软件,使root破解越来越容易.但是你思考过root破解的 原理吗?root破解的本质是什么呢?难道是利用了Lin ...
- 字符串-Alphabet
在一些应用当中,会对字符串的字母表进行限制,在这些应用中,往往会用到如下的示例所涉及到的几点知识: public static void main(String[] args){ String[] a ...