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

    }

  • 相关阅读:
    无涯教程-JavaScript - DEGREES函数
    自学CFD:我在实习岗速成无人机设计和仿真的故事
    python tkinter 使用
    不使用EF框架实现数据库增删改查
    06_Node.js服务器开发
    战队集结 蓄势而发 | 网安朝阳·西门子白帽黑客大赛
    AE 的软件、硬件、驱动控制、调试策略(没有算法)
    vmware esxi 7 直通GPU配置
    掉了两根头发后,我悟了!vue3的scoped原来是这样避免样式污染(上)
    分布式锁之防止超卖 --mysql原子操作,乐观锁,redis事务,乐观锁
  • 原文地址:https://blog.csdn.net/lfl18326162160/article/details/126949505