创建,插入,更新和删除:使用executeUpdate方法,而查询则用executeQuery

1.实例化FMDatabase

//paths: ios下Document路径,Document为ios中可读写的文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0]; //dbPath: 数据库路径,在Document中。
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"]; //创建数据库实例 db 这里说明下:如果路径中不存在"Test.db"的文件,sqlite会自动创建"Test.db"
FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;
if (![db open]) {
NSLog(@"Could not open db.");
return ;
}

2.创建表

//创建一个名为User的表,有两个字段分别为string类型的Name,integer类型的 Age

[db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];

3.插入

//插入数据使用OC中的类型 text对应为NSString integer对应为NSNumber的整形

[db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"Jeffery",[NSNumber numberWithInt:20]];

4.更新

//更新数据 

[db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"Hello",@"Jeffery"];

5.删除

//删除数据

[db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"Jeffery"];

6.查询

//返回数据库中第一条满足条件的结果
NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"10"]; //返回全部查询结果
FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"]; rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"10"]; while ([rs next]){
NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);
} [rs close];

备注:

需要提一点就是:线程安全

如果我们的app需要多线程操作数据库,那么就需要使用FMDatabaseQueue来保证线程安全了。 切记不能在多个线程中共同一个FMDatabase对象并且在多个线程中同时使用,这个类本身不是线程安全的,这样使用会造成数据混乱等问题。

使用FMDatabaseQueue很简单,首先用一个数据库文件地址来初使化FMDatabaseQueue,然后就可以将一个闭包(block)传入inDatabase方法中。 在闭包中操作数据库,而不直接参与FMDatabase的管理。

// 创建,最好放在一个单例的类中
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; // 使用
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; FMResultSet *rs = [db executeQuery:@"select * from foo"];
while ([rs next]) {
// …
}
}]; // 如果要支持事务
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; if (whoopsSomethingWrongHappened) {
*rollback = YES;
return;
}
// etc…
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
}];

附:

创建data类型用blob

创建表

[db executeUpdate:@"CREATE TABLE PersonList (Name text, Age integer, Sex integer, Phone text, Address text, Photo blob)"];

插入

[db executeUpdate:@"INSERT INTO PersonList (Name, Age, Sex, Phone, Address, Photo) VALUES (?,?,?,?,?,?)",
@"Jone", [NSNumber numberWithInt:], [NSNumber numberWithInt:], @“”, @“Taiwan, R.O.C”, [NSData dataWithContentsOfFile: filepath]];

FMDB增删查改的更多相关文章

  1. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  2. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  3. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  4. 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  5. jdbc的实例应用:增删查改实现

    //在jdbc中进行增删查改 //查看所有 public static void findAll() { String url = "jdbc:mysql://localhost:3306/ ...

  6. 用javascript实现html元素的增删查改[xyytit]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. hibernate基础增删查改简单实例

    hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...

  8. Entity FrameWork 增删查改的本质

    之前的文章里面已经说了,EF的增删查改.那时候的修改,删除,只能是先查询出来要修改的数据,再修改,删除...现在来一个改进版的,增删查改. 1.Add static void Add() { //1. ...

  9. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

随机推荐

  1. MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)

    供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...

  2. VS2010+PCL+openni配置

    PCL中文论坛:http://www.pclcn.org/bbs/forum.php 1.安装 pcl 的完全安装包可以到: http://pointclouds.org/downloads/wind ...

  3. 修改config.php配置

    $data=array( "name"=>"222222", "tel"=>159131, "address" ...

  4. XCode: duplicate symbol

    XCode: duplicate symbol 2013-05-27 17:28:21|  分类: iOS|字号 订阅     遇到引用库重复定义的问题,需要解决.   项目需要,同时引用ZBar和Q ...

  5. java数据结构之二叉树的实现

    java二叉树的简单实现,可以简单实现深度为n的二叉树的建立,二叉树的前序遍历,中序遍历,后序遍历输出. /** *数据结构之树的实现 *2016/4/29 * **/ package cn.Link ...

  6. HDU2216:Game III(BFS)

    Game III Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  7. CodeForces 687A NP-Hard Problem(二分图判定)

    这本来一个挺简单的题呢,结果让我给想复杂了,二分图就是把图分成了两部分,然后不同颜色各一边,肯定是满足题目中说的边和点的条件的,真是犯二了.. 代码如下: #include<iostream&g ...

  8. HDU 5718 Oracle

    如果非零的数小于等于1个,则无解.否则有解. 取出一个最小的非零的数作为一个数,剩下的作为一个数,相加即可. #include<cstdio> #include<cstring> ...

  9. USACO Section 1.3 Combination Lock 解题报告

    题目 题目描述 农夫John的牛从农场逃脱出去了,所以他决定用一个密码锁来把农场的门锁起来,这个密码锁有三个表盘,每个表盘都是环形的,而且上面刻有1~N,现在John设了一个开锁密码,而且这个锁的设计 ...

  10. PAT1064(上)分析部分

    Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B A Binary Search Tree (BST) ...