• FMDB增删查改


    创建,插入,更新和删除:使用executeUpdate方法,而查询则用executeQuery

    1.实例化FMDatabase

    //paths: ios下Document路径,Document为ios中可读写的文件夹
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
             
    //dbPath: 数据库路径,在Document中。
    NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];
             
    //创建数据库实例 db 这里说明下:如果路径中不存在"Test.db"的文件,sqlite会自动创建"Test.db"
    FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;
    if (![db open]) {
    NSLog(@"Could not open db.");
    return ;
    }


    2.创建表

    //创建一个名为User的表,有两个字段分别为string类型的Name,integer类型的 Age
    
    [db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];

    3.插入

    //插入数据使用OC中的类型 text对应为NSString integer对应为NSNumber的整形
    
    [db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"Jeffery",[NSNumber numberWithInt:20]];
    

    4.更新

    //更新数据 
    
    [db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"Hello",@"Jeffery"];
    

    5.删除

    //删除数据
    
    [db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"Jeffery"];
    

    6.查询

    //返回数据库中第一条满足条件的结果
    NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"10"];
                  
    //返回全部查询结果
    FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];
                  
    rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"10"];
                  
    while ([rs next]){
    NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);
    }
                  
    [rs close];
    

    备注:

    需要提一点就是:线程安全

    如果我们的app需要多线程操作数据库,那么就需要使用FMDatabaseQueue来保证线程安全了。 切记不能在多个线程中共同一个FMDatabase对象并且在多个线程中同时使用,这个类本身不是线程安全的,这样使用会造成数据混乱等问题。

    使用FMDatabaseQueue很简单,首先用一个数据库文件地址来初使化FMDatabaseQueue,然后就可以将一个闭包(block)传入inDatabase方法中。 在闭包中操作数据库,而不直接参与FMDatabase的管理。

    // 创建,最好放在一个单例的类中
    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
                        
    // 使用
    [queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
                        
        FMResultSet *rs = [db executeQuery:@"select * from foo"];
        while ([rs next]) {
            // …
        }
    }];
                        
    // 如果要支持事务
    [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
                        
        if (whoopsSomethingWrongHappened) {
            *rollback = YES;
            return;
        }
        // etc…
        [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
    }];
    

     附:

    创建data类型用blob

    创建表

    [db executeUpdate:@"CREATE TABLE PersonList (Name text, Age integer, Sex integer, Phone text, Address text, Photo blob)"];

    插入

    [db executeUpdate:@"INSERT INTO PersonList (Name, Age, Sex, Phone, Address, Photo) VALUES (?,?,?,?,?,?)",
    @"Jone", [NSNumber numberWithInt:20], [NSNumber numberWithInt:0], @“091234567”, @“Taiwan, R.O.C”, [NSData dataWithContentsOfFile: filepath]];
  • 相关阅读:
    修改 dll
    SQLServer中char、varchar、nchar、nvarchar的区别:
    关于破解的一点心得
    asp.net 操作XML
    jquery autocomplete
    【转】height,posHeight和pixelHeight区别
    异常处理 Access to the path is denied
    asp.net 获得客户端 mac 地址
    cmd 跟踪路由
    Excel 宏
  • 原文地址:https://www.cnblogs.com/wlsxmhz/p/6067291.html
Copyright © 2020-2023  润新知