iOS 新建本地数据库FMDB
1.在appdelegate.m中添加
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
myDelegate.dbPath = [self dataFilePath];
[self createTable];
return YES;
}
- (NSString *) dataFilePath//应用程序的沙盒路径
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *document = [path objectAtIndex:0];
return [document stringByAppendingPathComponent:@"NameData.sqlite"];
}
- (void)createTable
{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
NSFileManager *fileManager = [NSFileManager defaultManager];
FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];
if (![fileManager fileExistsAtPath:myDelegate.dbPath]) {
NSLog(@"还未创建数据库,现在正在创建数据库");
if ([db open]) {
[db executeUpdate:@"create table if not exists StudentList (name text, address text, id text)"];
[db close];
}else{
NSLog(@"database open error");
}
}
NSLog(@"FMDatabase:---------%@",db);
}
2.新增插入,更新,查询,删除操作
-(void)btnClick{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];
[db open];
NSString * name =@"梨子";
NSString *address = @"砀山";
int i = 1;
NSString *id = [NSString stringWithFormat:@"%d",i];
BOOL res = [db executeUpdate:@"INSERT INTO StudentList (name, address, id) VALUES (?, ?, ?)", name, address, id];
if (res == NO) {
NSLog(@"数据插入失败");
}else{
NSLog(@"数据插入成功");
}
[db close];
}
-(void)btn1Click{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];
[db open];
int i = 1;
NSString *id = [NSString stringWithFormat:@"%d",i];
BOOL res = [db executeUpdate:@"UPDATE StudentList SET name = ?, address = ? WHERE id = ?",@"葡萄",@"新疆",id];
if (res == NO) {
NSLog(@"数据修改失败");
}else{
NSLog(@"数据修改成功");
}
[db close];
}
-(void)btn2Click{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];
[db open];
int i = 1;
NSString *id = [NSString stringWithFormat:@"%d",i];
NSString *nameOut = [db stringForQuery:@"SELECT name FROM StudentList WHERE id = ?",id];
NSString *addressOut= [db stringForQuery:@"SELECT address FROM StudentList WHERE id = ?",id];
NSLog(@"查询到的名字===%@----地址===%@",nameOut,addressOut);
[db close];
}
-(void)btn3Click{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
FMDatabase *db = [FMDatabase databaseWithPath:myDelegate.dbPath];
[db open];
int i = 1;
NSString *id = [NSString stringWithFormat:@"%d",i];
BOOL res = [db executeUpdate:@"DELETE FROM StudentList WHERE id = ?",id];
if (res == NO) {
NSLog(@"删除数据失败");
}else{
NSLog(@"删除数据成功");
}
[db close];
}
iOS 新建本地数据库FMDB相关推荐
- 本地建mysql数据库_mysql 安装及新建本地数据库遇到的问题
如果你只需要连接远程数据库,那么不需要安装mysql,直接安装mysql workbench,通过workbench就可以连接远程数据库.但是如果你需要自己在本地建立一个数据库,那么你肯定就需要安装m ...
- Electron-Vue中操作本地数据库NeDB
NeDB是使用Node.js实现的一个NoSQL嵌入式数据库操作模块,可以充当内存数据库,也可以用来实现本地存储,甚至可以在浏览器中使用.查询方式比较灵活,支持使用正则.比较运算符.逻辑运算符.索引以 ...
- C# 实现安卓和iOS app 读写数据库,实现手机本地存储
一.向源代码添加SQLite 向解决方案添加NuGet包 右击解决方案 > 管理解决方案的NuGet 程序包 在"浏览"选项卡中搜索"sqlite-net-pcl& ...
- ios 查询mysql数据库操作系统_iOS数据库FMDB--增删改查(模糊查询)详细介绍
简介: 很早就想整理一下数据库的使用了,刚好最近接触较多,加之可以安排出空余的时间,所以瓜子我贡献出自己喝咖啡的时间整理一下FMDB的使用,以下是对FMDB的介绍以及基本使用 --- insert.d ...
- Windows Phone开发(48):不可或缺的本地数据库
原文:Windows Phone开发(48):不可或缺的本地数据库 也许WP7的时候,是想着让云服务露两手,故似乎并不支持本地数据库,所有数据都上传上"云"数据库中.不过呢,在SD ...
- 浏览器本地mysql_IndexedDB:浏览器里的本地数据库
IndexedDB 是什么 在现代浏览器的本地存储方案中,indexedDB 是一项重要的能力组成, 它是可以在浏览器端使用的本地数据库,可以存储大量数据,提供接口来查询,还可以建立索引,这些都是其他 ...
- iOS - 数据持久化之 FMDB 的使用
前言 上一篇文章「iOS - 使用 SQLite 数据库实现数据持久化」,介绍了如何使用 sqlite3 操作 SQLite 数据库实现增删改查.但是在代码编写的过程中,我们发现 sqlite3 需要 ...
- 在iOS开发中使用FMDB
在iOS开发中使用FMDB 前言 SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK 很早就支持了 SQLite,在使用时,只需 ...
- Exchange Server 2016管理系列课件39.新建本地移动请求
场景 通过新建本地移动请求,可以很方便的将用户邮箱从一个数据库迁移到另外一个数据库. 1)跨平台邮件迁移:(低exchange迁移到高版本exchange) 2)数据库平衡与优化:(企业当中,数据库规 ...
最新文章
- mysql oracle sqlit_【Go语言】连接数据库SQLite、MySQL、Oracle
- ServletContext 对象
- ★Kali信息收集~★6.Dmitry:汇总收集
- Spring发送带附件邮件
- SQL Server : 禁止在SQL Server中生成用户实例
- 均线交易策略的回测 r_使用r创建交易策略并进行回测
- vue.js computedmethod
- FabFilter Total Bundle 2021 mac版:14个专业的FabFilter插件(含安装与卸载)
- python3基础题目 100例_Python3.x 基础练习题100例(51-60)
- 【windows实用工具一】tftpd32+Xshell
- Shader编程教程_Shader新手入门视频教程_Shader编程从入门到精通
- 灰常实用的一键开关机电路,各位大佬进来mark一下?
- 全国一二线城市各大互联网(IT)公司,各位找工作的小伙伴可以参考
- 买好了虚拟服务器后怎么办,购买虚拟主机后应该怎么操作?
- 抓包分析,一条Linux命令实现路由器自动登录深大校园网认证(Drcom Pt版)
- 复合平面波超声成像(Matlab Field II仿真)
- java中modifier_java中关于.lang.reflect.Modifier.isInterface()方法的实例详解
- TexMaker(Latex编辑器)软件的“文献引用”操作:Xelatex方式运行的操作方法
- 360修复IE时候的注册表动作记录
- unique和primary的添加和修改