iOS: 学习笔记, 使用FMDatabase操作sqlite3
使用FMDatabase操作sqlite3数据库非常简单和方便
//
// main.m
// iOSDemo0602_sqlite3
//
// Created by yao_yu on 14-6-2.
// Copyright (c) 2014年 yao_yu. All rights reserved.
// #import <UIKit/UIKit.h>
#import "FMDatabase.h" void test01();
BOOL isfile(NSString *path);
BOOL isdir(NSString *path); int main(int argc, char * argv[])
{
@autoreleasepool {
test01();
}
return ;
} void test01()
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docpath = [paths objectAtIndex:];
NSString *dbpath = [docpath stringByAppendingPathComponent:@"YY/Data2"];
// NSLog(@"%@", dbpath);
if (!isdir(dbpath)) {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:dbpath withIntermediateDirectories:YES attributes:nil error:nil];
} NSString *dbfile = [dbpath stringByAppendingPathComponent:@"data.db"];
NSLog(@"%@", dbfile); //初始化
FMDatabase *db = [FMDatabase databaseWithPath:dbfile];
//打开数据库
[db open]; //删除表
[db executeUpdate:@"drop table persons"]; //添加表
[db executeUpdate:@"create table persons(id, name)"]; //删除原有数据
[db executeUpdate:@"delete from persons"]; //使用事务添加数据
[db beginTransaction];
for(int i=; i< ; i++)
{
[db executeUpdate:@"insert into persons values(?,?)", [NSString stringWithFormat:@"%d", i], [NSString stringWithFormat:@"第%d", i]];
}
[db commit]; //数据查询
FMResultSet *cursor = [db executeQuery:@"select * from persons"];
int nCols = cursor.columnCount;
while ([cursor next]) {
for (int i=; i<nCols; i++) {
printf("%s ", [[cursor stringForColumnIndex:i] UTF8String]);
}
printf("\n");
}
cursor = nil; //关闭数据库
[db close]; //删除数据库测试文件
[[NSFileManager defaultManager] removeItemAtPath:dbfile error:nil]; } BOOL isfile(NSString *path)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isdir = NO;
if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) {
return isdir == NO;
}
return NO;
} BOOL isdir(NSString *path)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isdir = NO;
if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) {
return isdir == YES;
}
return NO;
}
iOS: 学习笔记, 使用FMDatabase操作sqlite3的更多相关文章
- IOS学习笔记25—HTTP操作之ASIHTTPRequest
IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...
- IOS学习笔记25—HTTP操作之ASIHTTPRequest(一)
ASIHTTPRequest是一个第三方开源项目,在现在的IOS应用中多使用到这个开源类库来提供网络操作,相比于SDK提供的网络操作类库,ASIHTTPRequest使用上更加方便.效率更高,同时功能 ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
- iOS学习笔记17-FMDB
上一节我已经介绍了SQLite的简单使用,不了解的可以提前去看一下iOS学习笔记16-数据库SQLite,这节我们来讲下FMDB. 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQL ...
- iOS学习笔记16-数据库SQLite
一.数据库 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等.离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式: 归档:NSKeyedArchiver 偏好设置:NSU ...
- iOS学习笔记17-FMDB你好!
上一节我已经介绍了SQLite的简单使用,不了解的可以提前去看一下iOS学习笔记16-数据库SQLite,这节我们来讲下FMDB. 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQL ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
随机推荐
- PTA 06-图2 Saving James Bond - Easy Version (25分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- Cookie案例分析
一.案例- 显示用户上次访问的时间 当用户第一次访问该页面的时候提示:你好,你是第一次访问本页面,当前时间为:2016-11-3 22:10:30 第n次访问该页面时:欢迎回来,你上次访问的时间是:2 ...
- 七周七语言——Prolog(二)
1 递归 首先来看一个知识库: father(zeb,john_boy_sr). father(john_boy_sr,john_boy_jr). ancestor(X,Y):-father(X,Y ...
- 自定义toast功能
http://download.csdn.net/detail/caryt/8105031
- [Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two ...
- LabVIEW设计模式系列——普遍使用值改变事件
标准: 1.当使用值改变事件时,使用单击时触发或者单击时释放开关动作.这样即保证仅仅触发一次,也保证按钮恢复默认值 标准:1.值改变事件的优点:不论是鼠标动作还是键盘动作都能触发值改变事件,增强了程序 ...
- VC/MFC 下 递归遍历目录下的所有子目录及文件
在MFC下要实现文件夹的递归遍历,可用CFileFind类,依次读取文件夹下的子文件夹和文件,并判断通过判断是文件夹还是文件来决定递归遍历.递归遍历代码如下: /******************* ...
- Div+css中ul ol li dl dt dd使用
ol 有序列表.<ol><li>……</li><li>……</li><li>……</li></ol>表现 ...
- 使用Git操作GitHub代码入门教程
GitHub除了网页操作外,还可以借助本地客户端git(或github for windows)来增删修改远程代码.使用Git操作来连接GitHub可以通过Https或SSH方式,使用SSH方式可以免 ...
- JavaScript的DOM操作(三)
1.相关元素操作: var a = document.getElementById("id"); var b = a.nextSibling,找a的下一个同辈元素,注意空格 var ...