FMDB

作者: xiaocaoera | 来源:发表于2019-07-28 18:04 被阅读0次

    创建数据表

    NSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString*filename = [doc stringByAppingdingPathComponent:@"students.sqlte"];

    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filename];

    [queue inDatabase:^(FMDatabase *db) {

        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL"];

        if(rsult) { 

           NSLog(@"succeeded");

        }else{

        NSLog(@"failed");

        }

        }];

    插入数据

    FMDB中,除查询意外的所有操作,都称为“更新”。

    create, drop, insert, update, delete等

    [slef.queue inDatabase:^(FMDatabase *db){

        [db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);", "lucy", @(arc4random_uniform(40))];

        }];

    [slef.queue inDatabase:^(FMDatabase *db){

        [db excuteUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES (%@, %d);", name, arc4random_uniform(40)];

    ];

    查询

    [slef.queue inDatabase:^(FMDatabase *db){

        FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM t_student"];

        while([resultSet next]) {

        int ID= [resultSet intForColumn:@"id"];

        NSString*name = [resultSet stringForColumn:@"name"];

        int age = [resultSet intForColumn:@"age"];

        }

    ];

    删除表格

    [db executeUpdate:@"DROP TABLE IF EXISTS t_student"];

    相关文章

      网友评论

          本文标题:FMDB

          本文链接:https://www.haomeiwen.com/subject/nkeprctx.html