博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FMDB 基本使用
阅读量:7155 次
发布时间:2019-06-29

本文共 1744 字,大约阅读时间需要 5 分钟。

 
 
 // 0.拼接数据库存放的沙盒路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *sqlFilePath = [path stringByAppendingPathComponent:@"student.txt"];
   
    // 1.通过路径创建数据库
    FMDatabase * db = [FMDatabase databaseWithPath:sqlFilePath];
    self.db = db;
   
   // NSLog(@"%@\n%@",path,sqlFilePath);
   
    if ([self.db open]) {
        NSLog(@"打开成功");
        BOOL success = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER DEFAULT 1)"];
        if (success) {
            NSLog(@"创建表成功");
        } else {
            NSLog(@"创建表失败");
        }
    } else {
        NSLog(@"打开失败");
    }
 
 
 
//插入数据
- (void)addDataToSqlite{
    age++;
    BOOL success = [self.db executeUpdate:@"INSERT INTO t_student (name,age) VALUES (?,?);",@"jack",@(age)];
   
    if (success) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入失败");
    }
}
//删除数据
- (void)deleteDataFromSqlite{
    // 删除数据
    BOOL success = [self.db executeUpdate:@"DELETE FROM t_student WHERE age > 15 AND age < 25;"];
    if (success) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
//修改数据
- (void)changeDataFromSqlite{
    // 修改数据
    BOOL success = [self.db executeUpdate:@"UPDATE t_student SET name = 'liwx' WHERE age > 12 AND age < 15;"];
    if (success) {
        NSLog(@"修改成功");
    } else {
        NSLog(@"修改失败");
    }
}
//查询数据
- (void)watchDataFromSqlite{
    //如果要查询范围  在最后面添加  WHERE age > 15
    FMResultSet * result = [self.db executeQuery:@"SELECT id, name, age FROM t_student "];
    while ([result next]) {
        int ID = [result intForColumnIndex:0];
        NSString *name = [result stringForColumnIndex:1];
        int age_resule = [result intForColumn:@"age"];
       
        NSLog(@"--------ID: %zd, name: %@, age: %zd", ID, name, age_resule);
    }
}
 
 

FMDatabaseQueue数据库队列基本使用

使用FMDatabaseQueue类在多线程中执行多个查询或更新是线程安全的.
 
 

转载于:https://www.cnblogs.com/Jackie-subDai/p/9290192.html

你可能感兴趣的文章
取消word中所有超链接
查看>>
AABB边框、OBB边框、通过比较球包围
查看>>
Atitit. 软件开发中的管理哲学--一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向...
查看>>
JS产生随机数的几个用法!
查看>>
浏览器默认样式(User Agent Stylesheet)
查看>>
所有Mac用户都需要知道的9个实用终端命令行
查看>>
Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成
查看>>
Java虚拟机详解04----GC算法和种类【重要】
查看>>
Receiver type for instance message is a forward
查看>>
将SALT_STACK的JOB-CACHE放到数据库中,而建库用DJANGO的ORM完成
查看>>
GPIO推挽输出和开漏输出详解
查看>>
I18N、L10N、G11N
查看>>
引用类中的enum
查看>>
影响网站打开速度的9大因素
查看>>
HTML5之废弃和更新的元素与属性
查看>>
[转]asp.net解决高并发的方案.
查看>>
(转)unity中基于alpha通道的shadow volume实现
查看>>
linux下svn的co如何排除目录
查看>>
项目中最常用到的颜色
查看>>
[转]10个学习Android开发的网站推荐
查看>>