【代码笔记】iOS-FMDBDemo
一,效果图。

二,工程图。

三,代码。
ViewController.h

#import <UIKit/UIKit.h>
#import "FMDatabase.h"
#import "FMDatabaseQueue.h" @interface ViewController : UIViewController
{
FMDatabase *db;
NSString *database_path; }
@end

ViewController.m

#import "ViewController.h" #define DBNAME @"personinfo.sqlite"
#define ID @"id"
#define NAME @"name"
#define AGE @"age"
#define ADDRESS @"address"
#define TABLENAME @"PERSONINFO" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //初始化数据库存放目录
[self addDocumentPath];
//初始化界面
[self addView]; [super viewDidLoad]; }
#pragma -mark -functions
//初始化数据库存放目录
-(void)addDocumentPath
{
//获得Documents目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSLog(@"--documents--%@",documents);
//添加/号,使之变成一个完整的路径
database_path = [documents stringByAppendingPathComponent:DBNAME];
NSLog(@"--database_path---%@",database_path);
db = [FMDatabase databaseWithPath:database_path];
}
//初始化用户界面
-(void)addView
{ //新建数据库
UIButton *createBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
createBtn.frame=CGRectMake(60, 60, 200, 50);
[createBtn addTarget:self action:@selector(doClickCreateButton) forControlEvents:UIControlEventTouchUpInside];
[createBtn setTitle:@"createTable" forState:UIControlStateNormal];
[self.view addSubview:createBtn]; //插入数据库
UIButton *insterBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
insterBtn.frame=CGRectMake(60, 130, 200, 50);
[insterBtn addTarget:self action:@selector(doClickInsertButton) forControlEvents:UIControlEventTouchUpInside];
[insterBtn setTitle:@"insert" forState:UIControlStateNormal];
[self.view addSubview:insterBtn]; //更新数据库
UIButton *updateBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
updateBtn.frame=CGRectMake(60, 200, 200, 50);
[updateBtn addTarget:self action:@selector(doClickUpdateButton) forControlEvents:UIControlEventTouchUpInside];
[updateBtn setTitle:@"update" forState:UIControlStateNormal];
[self.view addSubview:updateBtn]; //删除数据库
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteBtn.frame=CGRectMake(60, 270, 200, 50);
[deleteBtn addTarget:self action:@selector(doClickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
[deleteBtn setTitle:@"delete" forState:UIControlStateNormal];
[self.view addSubview:deleteBtn]; //查看数据库
UIButton *selectBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
selectBtn.frame=CGRectMake(60, 340, 200, 50);
[selectBtn addTarget:self action:@selector(doClickSelectButton) forControlEvents:UIControlEventTouchUpInside];
[selectBtn setTitle:@"select" forState:UIControlStateNormal];
[self.view addSubview:selectBtn]; //多线程
UIButton *multithreadBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
multithreadBtn.frame=CGRectMake(60, 410, 200, 50);
[multithreadBtn addTarget:self action:@selector(doClickMultithreadButton) forControlEvents:UIControlEventTouchUpInside];
[multithreadBtn setTitle:@"multithread" forState:UIControlStateNormal];
[self.view addSubview:multithreadBtn]; }
#pragma -mark -doClickAction
//新建数据库
- (void)doClickCreateButton{
//sql 语句
if ([db open]) { NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
BOOL res = [db executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close]; }
} //插入数据库
-(void)doClickInsertButton{
if ([db open]) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"张三", @"13", @"济南"];
BOOL res = [db executeUpdate:insertSql1];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
} NSString *insertSql2 = [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"济南"];
BOOL res2 = [db executeUpdate:insertSql2]; if (!res2) {
NSLog(@"error when insert db table");
}else{
NSLog(@"success to insert db table");
}
[db close]; } }
//修改数据库
-(void)doClickUpdateButton{
if ([db open]) {
NSString *updateSql = [NSString stringWithFormat:
@"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",
TABLENAME, AGE, @"15" ,AGE, @"13"];
BOOL res = [db executeUpdate:updateSql];
if (!res) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
[db close]; } }
//删除数据库
-(void)doClickDeleteButton{
if ([db open]) { NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where %@ = '%@'",
TABLENAME, NAME, @"张三"];
BOOL res = [db executeUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close]; } }
//查看数据库
-(void)doClickSelectButton{ if ([db open]) {
NSString * sql = [NSString stringWithFormat:
@"SELECT * FROM %@",TABLENAME];
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
int Id = [rs intForColumn:ID];
NSString * name = [rs stringForColumn:NAME];
NSString * age = [rs stringForColumn:AGE];
NSString * address = [rs stringForColumn:ADDRESS];
NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address);
}
[db close];
}
}
//多线程操作数据库
-(void)doClickMultithreadButton{ FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:database_path];
dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL); dispatch_async(q1, ^{
for (int i = 0; i < 50; ++i) {
[queue inDatabase:^(FMDatabase *db2) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",
TABLENAME, NAME, AGE, ADDRESS];
NSString * name = [NSString stringWithFormat:@"jack %d", i];
NSString * age = [NSString stringWithFormat:@"%d", 10+i]; BOOL res = [db2 executeUpdate:insertSql1, name, age,@"济南"];
if (!res) {
NSLog(@"error to inster data: %@", name);
} else {
NSLog(@"succ to inster data: %@", name);
}
}];
}
}); dispatch_async(q2, ^{
for (int i = 0; i < 50; ++i) {
[queue inDatabase:^(FMDatabase *db2) {
NSString *insertSql2= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",
TABLENAME, NAME, AGE, ADDRESS]; NSString * name = [NSString stringWithFormat:@"lilei %d", i];
NSString * age = [NSString stringWithFormat:@"%d", 10+i]; BOOL res = [db2 executeUpdate:insertSql2, name, age,@"北京"];
if (!res) {
NSLog(@"error to inster data: %@", name);
} else {
NSLog(@"succ to inster data: %@", name);
}
}];
}
}); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【代码笔记】iOS-FMDBDemo的更多相关文章
- IOS开发笔记 IOS如何访问通讯录
IOS开发笔记 IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] ...
- 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程
一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...
- 【hadoop代码笔记】hadoop作业提交之汇总
一.概述 在本篇博文中,试图通过代码了解hadoop job执行的整个流程.即用户提交的mapreduce的jar文件.输入提交到hadoop的集群,并在集群中运行.重点在代码的角度描述整个流程,有些 ...
- 【Hadoop代码笔记】目录
整理09年时候做的Hadoop的代码笔记. 开始. [Hadoop代码笔记]Hadoop作业提交之客户端作业提交 [Hadoop代码笔记]通过JobClient对Jobtracker的调用看详细了解H ...
- 笔记-iOS 视图控制器转场详解(上)
这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...
- <Python Text Processing with NLTK 2.0 Cookbook>代码笔记
如下是<Python Text Processing with NLTK 2.0 Cookbook>一书部分章节的代码笔记. Tokenizing text into sentences ...
- [学习笔记] SSD代码笔记 + EifficientNet backbone 练习
SSD代码笔记 + EifficientNet backbone 练习 ssd代码完全ok了,然后用最近性能和速度都非常牛的Eifficient Net做backbone设计了自己的TinySSD网络 ...
- DW网页代码笔记
DW网页代码笔记 1.样式. class 插入类样式 标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术 解决页面动态交互问题<form> ...
- 前端学习:JS(面向对象)代码笔记
前端学习:JS(面向对象)代码笔记 前端学习:JS面向对象知识学习(图解) 创建类和对象 创建对象方式1调用Object函数 <body> </body> <script ...
- 离屏渲染学习笔记 /iOS圆角性能问题
离屏渲染学习笔记 一.概念理解 OpenGL中,GPU屏幕渲染有以下两种方式: On-Screen Rendering 意为当前屏幕渲染,指的是GPU的渲染操作是在当前用于显示的屏幕缓冲区中进行. O ...
随机推荐
- Eclipse的使用与Oblect类的常用方法_DAY11
一.Java开发工具的使用 A:notepad windows自带的记事本. B:高级记事本 Editplus Notepad++ UE sublime2 C:集成开发工具(IDE) 开发和运行. E ...
- (转)30 个实例详解 TOP 命令
原文:http://blog.jobbole.com/112873/?utm_source=blog Linux中的top命令显示系统上正在运行的进程.它是系统管理员最重要的工具之一.被广泛用于监视服 ...
- Git for Windows之基础环境搭建与基础操作
一.安装Git工具 下载地址:Git For Windows 下载完后,安装,全程Next. 二.全局配置 1.配置本地用户名,用于提交代码 2.配置邮箱 三.创建本地Git项目仓库 1.建立代码仓库 ...
- 打印页面时a标签不显示URL的方法
以前写博客啊,总想写一篇大作,然后希望能挂到博客园首页,隔一会儿看看阅读量有多少.其实哪有那么多大作,大部分时间都是解决了一个小问题,然后需要记录一下.比如下面这篇. 今天遇到一个需求是,打印网页时, ...
- 一口一口吃掉Hexo(五)
如果你想得到更好的阅读效果,请访问我的个人网站 ,版权所有,未经许可不得转载! 通过前四节的内容,相信你已经能够在你的虚拟主机上成功部署网站,并且能够通过你自己的域名访问你的网站了,接下来要做的就是日 ...
- error C4996: 'scanf': This function or variable may be unsafe.
项目属性-配置属性-c/c++-预处理器- 在下面的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS 添加完成后应用并退出 http://jingyan.baidu.com/al ...
- 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装用来定时任务apscheduler库(图文详解)
不多说,直接上干货! Anaconda2 里 PS C:\Anaconda2\Scripts> PS C:\Anaconda2\Scripts> pip.exe install apsc ...
- 《垃圾回收的算法与实现》——Python垃圾回收
Python垃圾回收 python采用引用计数法进行垃圾回收 Python内存分配 python在分配内存空间时,在malloc之上堆放了3个独立的分层. python内存分配时主要由arena.po ...
- Python做int()强制类型转换的时候,小数是如何取舍的?
白月黑羽今天给大家分享一个冷知识:) int()强制类型转换小数是如何取舍的? 使用 int() 将小数转换为整数,小数取整会采用比较暴力的截断方式,即小数点后面的会被强制舍去,向下取整. 例如:5. ...
- pyenv docter检测出configure: error: OpenSSL is not installed.解决方案
1 在安装相应版本的python时,前声明 CFLAGS=-I/usr/include/openssl \ LDFLAGS=-L/usr/lib64 \ pyenv install -v 3.5.1