美文网首页
sqlite3FMDB

sqlite3FMDB

作者: 赤焰军少帅林殊 | 来源:发表于2017-10-26 16:12 被阅读6次

    附上 github链接https://github.com/15118130757/Fmdbuser
    创建表

    //拼接数据库的路径/Documents/fmdb.sqlite
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"fmdb.sqlite"];
        //数据库文件
        self.db = [FMDatabase databaseWithPath:dbPath];
        //连接数据库文件
        if ([self.db open]) {
            //创建表
            BOOL isSuccess = [self.db executeUpdate:@"create table if not exists Student (id integer primary key, name text, score real)"];
            if (!isSuccess) {
                NSLog(@"创建不成功==%@", [self.db lastError]);
            }
            NSLog(@"创建成功%@",dbPath);
        }
        //关闭数据库文件
        [self.db close];
    

    插入值

    if ([self.db open]) {
            //带有参数的插入SQL语句
            BOOL isSuccess = [self.db executeUpdateWithFormat:@"insert into Student (id,name, score) values (%ld,%@, %f)",15118130757,@"梁亚海", 120.0];
            if (!isSuccess) {
                NSLog(@"插入数据失败:%@", [self.db lastError]);
            }
            
            [self.db executeUpdateWithFormat:@"insert into Student (id,name, score) values (%ld,%@, %f)",15060193253,@"旋涡鸣人", 99.0];
              [self.db executeUpdateWithFormat:@"insert into Student (id,name, score) values (%ld,%@, %f)",13682631295,@"旋涡鸣人", 98.0];
    
        }
        [self.db close];
    

    查询

       if ([self.db open]) {
            //查询(executeQuery)
    //        FMResultSet *resultSet = [self.db executeQuery:@"select * from Student"];//查询所有
    //        FMResultSet *resultSet=[self.db executeQuery:@"select * from Student where id=15118130757"];//查询数据库某一条数据
       FMResultSet *resultSet=[self.db executeQuery:@"select * from Student where name='旋涡鸣人'and id=15060193253"];//查询数据库某一条数据 多增加一个条件
            //循环
            while ([resultSet next]) {
                //选择方法
                NSString *nameStr = [resultSet stringForColumn:@"name"];
                double score = [resultSet doubleForColumn:@"score"];
                NSLog(@"%@,%f",nameStr,score);
            }
    
        }
        [self.db close];
    

    删除

     if ([self.db open]) {
            BOOL isSuccess = [self.db executeUpdate:@"delete from Student where id=13682631295"];
            if (!isSuccess) {
                NSLog(@"删除失败:%@", [self.db lastError]);
            }
        }
        [self.db close];
    

    更改

        if ([self.db open]) {
            BOOL isSuccess = [self.db executeUpdate:@"update Student set score=1028 where id=15118130757"];
            if (!isSuccess) {
                NSLog(@"修改失败:%@", [self.db lastError]);
            }
        }
        [self.db close];
    
    

    增加数据库字段

     if ([self.db open]) {
            //判断字段是否存在
            if (![self.db columnExists:@"age" inTableWithName:@"Student"]) {
                
                NSString *sql = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@",@"Student",@"age"];
                BOOL ret = [self.db executeUpdate:sql];
                if (ret  != YES) {
                    NSLog(@"add albumName fail");
                }
            }
            [self.db close];
        }
    

    相关文章

      网友评论

          本文标题:sqlite3FMDB

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