iOS 使用FMDB进行数据库操作

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

  1. 首先要先导入第三方类库FMdatabase。  
  2. 获得存放数据库文件的沙盒地址。 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
        +(NSString*)databaseFilePath 
     
    [objc] view plaincopy
     
            
               
            NSArray*filePath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
            NSString*documentPath=[filePathobjectAtIndex:0]; 
            NSLog(@"%@",filePath); 
            NSString*dbFilePath=[documentPathstringByAppendingPathComponent:@"db.sqlite"]; 
            returndbFilePath; 
               
            
        3、创建数据库的操作 
           
        +(void)creatDatabase 
            
            db=[[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]]retain]; 
            
        4、创建表 
           
        +(void)creatTable 
            
            //先判断数据库是否存在,如果不存在,创建数据库 
            if(!db){ 
            [selfcreatDatabase]; 
            
            //判断数据库是否已经打开,如果没有打开,提示失败 
            if(![dbopen]){ 
            NSLog(@"数据库打开失败"); 
            return
            
               
            //为数据库设置缓存,提高查询效率 
            [dbsetShouldCacheStatements:YES]; 
               
            //判断数据库中是否已经存在这个表,如果不存在则创建该表 
            if(![dbtableExists:@"people"]) 
            
            [dbexecuteUpdate:@"CREATETABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, nameTEXT, age INTEGER) "]; 
               
               
            NSLog(@"创建完成"); 
            
               
            
        5、增加表数据 
           
        +(void)insertPeople:(People*)aPeople 
            
            if(!db){ 
            [selfcreatDatabase]; 
            
               
            if(![dbopen]){ 
            NSLog(@"数据库打开失败"); 
            return
            
               
            [dbsetShouldCacheStatements:YES]; 
               
            if(![dbtableExists:@"people"]) 
            
            [selfcreatTable]; 
            
            //以上操作与创建表是做的判断逻辑相同 
            //现在表中查询有没有相同的元素,如果有,做修改操作 
            FMResultSet*rs=[dbexecuteQuery:@"select* from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]]; 
            if([rsnext]) 
            
            NSLog(@"dddddslsdkien"); 
            [dbexecuteUpdate:@"updatepeople set name = ?, age = ? where people_id =1",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; 
            
            //向数据库中插入一条数据 
            else
            [dbexecuteUpdate:@"INSERTINTO people (name, age) VALUES(?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; 
            
               
            
        6、删除数据 
           
        +(void)deletePeopleByID:(int)ID 
            
            if(!db){ 
            [selfcreatDatabase]; 
            
               
            if(![dbopen]){ 
            NSLog(@"数据库打开失败"); 
            return
            
               
            [dbsetShouldCacheStatements:YES]; 
               
            //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return 
            if(![dbtableExists:@"people"]) 
            
            return
            
            //删除操作 
            [dbexecuteUpdate:@"deletefrom people where people_id = ?", [NSStringstringWithFormat:@"%d",ID]]; 
               
            [dbclose]; 
            
        7、修改操作与增加操作的步骤一致 
           
        +(NSArray*)getAllPeople 
            
               
            if(!db){ 
            [selfcreatDatabase]; 
            
               
            if(![dbopen]){ 
            NSLog(@"数据库打开失败"); 
            returnnil; 
            
               
            [dbsetShouldCacheStatements:YES]; 
               
            if(![dbtableExists:@"people"]) 
            
            returnnil; 
            
               
            //定义一个可变数组,用来存放查询的结果,返回给调用者 
            NSMutableArray*peopleArray=[[NSMutableArrayalloc]initWithArray:0]; 
            //定义一个结果集,存放查询的数据 
            FMResultSet*rs=[dbexecuteQuery:@"select* from people"]; 
            //判断结果集中是否有数据,如果有则取出数据 
            while([rsnext]){ 
            People*aPeople=[[Peoplealloc]init]; 
               
            aPeople.peopleID=[rsintForColumn:@"people_id"]; 
            aPeople.name=[rsstringForColumn:@"name"]; 
            aPeople.age=[rsintForColumn:@"age"]; 
            //将查询到的数据放入数组中。 
            [peopleArrayaddObject:aPeople]; 
            
            return[peopleArrayautorelease]; 
            
        8、查询 
           
        +(NSArray*)getAllPeople 
            
               
            if(!db){ 
            [selfcreatDatabase]; 
            
               
            if(![dbopen]){ 
            NSLog(@"数据库打开失败"); 
            returnnil; 
            
               
            [dbsetShouldCacheStatements:YES]; 
               
            if(![dbtableExists:@"people"]) 
            
            returnnil; 
            
               
            //定义一个可变数组,用来存放查询的结果,返回给调用者 
            NSMutableArray*peopleArray=[[NSMutableArrayalloc]initWithArray:0]; 
            //定义一个结果集,存放查询的数据 
            FMResultSet*rs=[dbexecuteQuery:@"select* from people"]; 
            //判断结果集中是否有数据,如果有则取出数据 
            while([rsnext]){ 
            People*aPeople=[[Peoplealloc]init]; 
               
            aPeople.peopleID=[rsintForColumn:@"people_id"]; 
            aPeople.name=[rsstringForColumn:@"name"]; 
            aPeople.age=[rsintForColumn:@"age"]; 
            //将查询到的数据放入数组中。 
            [peopleArrayaddObject:aPeople]; 
            
            return[peopleArrayautorelease]; 
            }