• 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];

    }

  • 相关阅读:
    Redis 主从搭建和哨兵搭建
    李开复创业公司零一万物开源迄今为止最长上下文大模型:Yi-6B和Yi-34B,支持200K超长上下文
    求学之路五、六月的Review
    数据可视化设计经验分享,正确使用主题风格,打造专属数字大屏
    软件配置管理计划
    Java网络编程
    ubuntu2204任务栏显示cpu 网速信息
    第 308 场周赛 t4 6163. 给定条件下构造矩阵 拓扑排序
    JAVA传值和传引用的区别
    C#中的CSV文件读写
  • 原文地址:https://blog.csdn.net/lfl18326162160/article/details/126949505