中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS 使用FMDB進(jìn)行數(shù)據(jù)庫(kù)操作

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
  1. 首先要先導(dǎo)入第三方類庫(kù)FMdatabase。  
  2. 獲得存放數(shù)據(jù)庫(kù)文件的沙盒地址。 
        +(NSString*)databaseFilePath  
    
    [objc] view plaincopy
    
            {  
              
            NSArray*filePath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
            NSString*documentPath=[filePathobjectAtIndex:0];  
            NSLog(@"%@",filePath);  
            NSString*dbFilePath=[documentPathstringByAppendingPathComponent:@"db.sqlite"];  
            returndbFilePath;  
              
            }  
        3、創(chuàng)建數(shù)據(jù)庫(kù)的操作  
          
        +(void)creatDatabase  
            {  
            db=[[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]]retain];  
            }  
        4、創(chuàng)建表  
          
        +(void)creatTable  
            {  
            //先判斷數(shù)據(jù)庫(kù)是否存在,如果不存在,創(chuàng)建數(shù)據(jù)庫(kù)  
            if(!db){  
            [selfcreatDatabase];  
            }  
            //判斷數(shù)據(jù)庫(kù)是否已經(jīng)打開,如果沒(méi)有打開,提示失敗  
            if(![dbopen]){  
            NSLog(@"數(shù)據(jù)庫(kù)打開失敗");  
            return;  
            }  
              
            //為數(shù)據(jù)庫(kù)設(shè)置緩存,提高查詢效率  
            [dbsetShouldCacheStatements:YES];  
              
            //判斷數(shù)據(jù)庫(kù)中是否已經(jīng)存在這個(gè)表,如果不存在則創(chuàng)建該表  
            if(![dbtableExists:@"people"])  
            {  
            [dbexecuteUpdate:@"CREATETABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, nameTEXT, age INTEGER) "];  
              
              
            NSLog(@"創(chuàng)建完成");  
            }  
              
            }  
        5、增加表數(shù)據(jù)  
          
        +(void)insertPeople:(People*)aPeople  
            {  
            if(!db){  
            [selfcreatDatabase];  
            }  
              
            if(![dbopen]){  
            NSLog(@"數(shù)據(jù)庫(kù)打開失敗");  
            return;  
            }  
              
            [dbsetShouldCacheStatements:YES];  
              
            if(![dbtableExists:@"people"])  
            {  
            [selfcreatTable];  
            }  
            //以上操作與創(chuàng)建表是做的判斷邏輯相同  
            //現(xiàn)在表中查詢有沒(méi)有相同的元素,如果有,做修改操作  
            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]];  
            }  
            //向數(shù)據(jù)庫(kù)中插入一條數(shù)據(jù)  
            else{  
            [dbexecuteUpdate:@"INSERTINTO people (name, age) VALUES(?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]];  
            }  
              
            }  
        6、刪除數(shù)據(jù)  
          
        +(void)deletePeopleByID:(int)ID  
            {  
            if(!db){  
            [selfcreatDatabase];  
            }  
              
            if(![dbopen]){  
            NSLog(@"數(shù)據(jù)庫(kù)打開失敗");  
            return;  
            }  
              
            [dbsetShouldCacheStatements:YES];  
              
            //判斷表中是否有指定的數(shù)據(jù), 如果沒(méi)有則無(wú)刪除的必要,直接return  
            if(![dbtableExists:@"people"])  
            {  
            return;  
            }  
            //刪除操作  
            [dbexecuteUpdate:@"deletefrom people where people_id = ?", [NSStringstringWithFormat:@"%d",ID]];  
              
            [dbclose];  
            }  
        7、修改操作與增加操作的步驟一致  
          
        +(NSArray*)getAllPeople  
            {  
              
            if(!db){  
            [selfcreatDatabase];  
            }  
              
            if(![dbopen]){  
            NSLog(@"數(shù)據(jù)庫(kù)打開失敗");  
            returnnil;  
            }  
              
            [dbsetShouldCacheStatements:YES];  
              
            if(![dbtableExists:@"people"])  
            {  
            returnnil;  
            }  
              
            //定義一個(gè)可變數(shù)組,用來(lái)存放查詢的結(jié)果,返回給調(diào)用者  
            NSMutableArray*peopleArray=[[NSMutableArrayalloc]initWithArray:0];  
            //定義一個(gè)結(jié)果集,存放查詢的數(shù)據(jù)  
            FMResultSet*rs=[dbexecuteQuery:@"select* from people"];  
            //判斷結(jié)果集中是否有數(shù)據(jù),如果有則取出數(shù)據(jù)  
            while([rsnext]){  
            People*aPeople=[[Peoplealloc]init];  
              
            aPeople.peopleID=[rsintForColumn:@"people_id"];  
            aPeople.name=[rsstringForColumn:@"name"];  
            aPeople.age=[rsintForColumn:@"age"];  
            //將查詢到的數(shù)據(jù)放入數(shù)組中。  
            [peopleArrayaddObject:aPeople];  
            }  
            return[peopleArrayautorelease];  
            }  
        8、查詢  
          
        +(NSArray*)getAllPeople  
            {  
              
            if(!db){  
            [selfcreatDatabase];  
            }  
              
            if(![dbopen]){  
            NSLog(@"數(shù)據(jù)庫(kù)打開失敗");  
            returnnil;  
            }  
              
            [dbsetShouldCacheStatements:YES];  
              
            if(![dbtableExists:@"people"])  
            {  
            returnnil;  
            }  
              
            //定義一個(gè)可變數(shù)組,用來(lái)存放查詢的結(jié)果,返回給調(diào)用者  
            NSMutableArray*peopleArray=[[NSMutableArrayalloc]initWithArray:0];  
            //定義一個(gè)結(jié)果集,存放查詢的數(shù)據(jù)  
            FMResultSet*rs=[dbexecuteQuery:@"select* from people"];  
            //判斷結(jié)果集中是否有數(shù)據(jù),如果有則取出數(shù)據(jù)  
            while([rsnext]){  
            People*aPeople=[[Peoplealloc]init];  
              
            aPeople.peopleID=[rsintForColumn:@"people_id"];  
            aPeople.name=[rsstringForColumn:@"name"];  
            aPeople.age=[rsintForColumn:@"age"];  
            //將查詢到的數(shù)據(jù)放入數(shù)組中。  
            [peopleArrayaddObject:aPeople];  
            }  
            return[peopleArrayautorelease];  
            }  


標(biāo)簽: 數(shù)據(jù)庫(kù)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:python判斷遠(yuǎn)程端口是否打開

下一篇:Android獲取應(yīng)用程序下所有Activity