国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

IOS開發(fā)之?dāng)?shù)據(jù)庫(kù)FMDB

2019-11-14 19:13:59
字體:
供稿:網(wǎng)友

IOS開發(fā)之數(shù)據(jù)庫(kù)FMDB

1.簡(jiǎn)介

  需求作用:    

    如果需要保存大量的結(jié)構(gòu)較為復(fù)雜的數(shù)據(jù)時(shí)候, 使用數(shù)據(jù)庫(kù), 例如交規(guī)考試項(xiàng)目

  常用的數(shù)據(jù)庫(kù):

    (1)Microsoft SQL Server 2000/2008, 中小企業(yè)使用較多

    (2)Oracle 比較復(fù)雜, 大企業(yè)使用較多

    (3)MySQL數(shù)據(jù)庫(kù), 網(wǎng)站使用較多

    (4)sqlite:   本地?cái)?shù)據(jù)庫(kù), 訪問數(shù)據(jù)足夠快, 直接訪問文件

           足夠簡(jiǎn)單, 功能相對(duì)其他數(shù)據(jù)庫(kù)軟件不是特別齊全, 足夠用了

             足夠小, 系統(tǒng)不超過1M, 適合在移動(dòng)端上使用

2. MesaSQlite使用

實(shí)例:  使用數(shù)據(jù)存儲(chǔ)存儲(chǔ)一個(gè)班上學(xué)生的信息

      學(xué)號(hào)sid  用戶名username  密碼passWord 成績(jī)score

      1501    zhangsan      123      100

      1502    lilei         321      90

      1503    wangwu        222      80 

(1)創(chuàng)建數(shù)據(jù)庫(kù) 

  打開MesaSQlite后如果選擇Cancel,這就是需要你手動(dòng)創(chuàng)建一個(gè)屬于你的數(shù)據(jù)庫(kù),

  選擇File-->New Database ,彈出對(duì)話框Save 填寫保存的文件名和位置,

(2)創(chuàng)建數(shù)據(jù)表

(3)設(shè)計(jì)數(shù)據(jù)表(添加多個(gè)字段/列)

(4)數(shù)據(jù)庫(kù)常用操作

  增,刪,改,查

 

3.SQL結(jié)構(gòu)化查詢語句

SQL Structure Query Language, 結(jié)構(gòu)化查詢語言, 作用就是操作數(shù)據(jù)庫(kù)(創(chuàng)建表, 數(shù)據(jù)增刪改查)

1)創(chuàng)建數(shù)據(jù)表

create table if not exists StudentInfo(sid integer, username varchar(20), password varchar(20),score varchar(20))

(2)插入數(shù)據(jù)

insert into StudentInfo(sid,username,password,score) values(1503,'wangwu','222','80')

(3)查詢數(shù)據(jù)

  <1>查詢表格中的所有數(shù)據(jù)

select * from StudentInfo;

  <2>查詢指定的字段

    例:查詢所有名字username

select username from StudentInfo

  <3>根據(jù)指定的條件進(jìn)行查詢

    例:查找username為zhangsan的所有信息

select * from StudentInfo where username='zhangsan'

  <4>根據(jù)多個(gè)條件進(jìn)行查詢

    例:查找username為zhangsan,并且password為123的所有信息

select * from StudentInfo where username='zhangsan' and password='123'

  <5>查詢后按需要排序

    例:根據(jù)age升序排序

      降序排列

select * from StudentInfo order by score 

      升序排列

select * from StudentInfo order by score desc

 

  <6>獲取,查詢數(shù)據(jù)的行數(shù)或個(gè)數(shù)

select count(*) from StudentInfo 

 

(4)修改數(shù)據(jù)

update StudentInfo set score='100'  where username='zhangsan';

(5)刪除數(shù)據(jù)

delete from StudentInfo where sid='1503'

 

4. FMDB操作數(shù)據(jù)庫(kù)

(1)配置 (進(jìn)行簡(jiǎn)單的配置即可使用)

  導(dǎo)入文件,

  添加二進(jìn)制庫(kù) libsqlite3.dylib,

  包含頭文件#import "FMDatabase.h"

    注意:說明一下iOS的安全機(jī)制————沙盒機(jī)制

//沙盒機(jī)制-(void)SandBox{    //IOS安全機(jī)制 - 沙盒    //(1)每個(gè)應(yīng)用內(nèi)容度放在一個(gè)沙盒目錄下面    //(2)每個(gè)應(yīng)用只能修改自己沙盒目錄下得文件,其他應(yīng)用文件無法修改    //(3)默認(rèn)文件Documents,Library,tmp    //開發(fā):自己創(chuàng)建的文件放在Documents下面        //確定文件位置    //當(dāng)前應(yīng)用文件夾 :NSHomeDirectory()    NSLog(@"%@",[[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSHomeDirectory() error:nil]);    NSLog(@"home == %@",NSHomeDirectory());}

 

(2)使用

  實(shí)例:存儲(chǔ)學(xué)生的信息

    a.創(chuàng)建數(shù)據(jù)庫(kù)

-(void)creatAndInitFMDBDatabase{    //設(shè)置路徑    NSString *path = [NSString stringWithFormat:@"%@/Documents/stuInfo.sqlite",NSHomeDirectory()];        //創(chuàng)建數(shù)據(jù)庫(kù)(如果不存在則創(chuàng)建打開,如果存在則直接打開)    _database = [[FMDatabase alloc]initWithPath:path];    if (!_database.open) {        NSLog(@"失敗");        return;    }    NSLog(@"成功");}

    

    b.創(chuàng)建數(shù)據(jù)表

-(void)createTable{    //executeQuery用來執(zhí)行select語句    //其他語句使用executeUpdate    //    _database executeQuery:<#(NSString *), ...#>    NSString *sql = @"create table if not exists StudentInfo(sid integer,username varchar(20),password varchar(20),score varchar(20))";    BOOL b = [_database executeUpdate:sql];    NSLog(@"creatTable = %d",b);}

 

    c.插入數(shù)據(jù)

-(void)insertData{    int sid = 1501;    NSString *username = @"zhangsan";    NSString *password = @"123";    NSString *score = @"100";    NSString *sql = @"insert into StudentInfo(sid,username,password,score) values(?,?,?,?)";        BOOL b = [_database executeUpdate:sql,[NSString stringWithFormat:@"%d",sid],username,password,score];    NSLog(@"insertData = %d",b);}

 

         d.查詢數(shù)據(jù)

-(void)queryData{    NSString *sql = @"select * from StudentInfo";    FMResultSet *resultSet = [_database executeQuery:sql];    while ([resultSet next]) {        NSLog(@"sid = %@, username = %@, password = %@, score = %@",[resultSet stringForColumn:@"sid"],[resultSet stringForColumn:@"username"],[resultSet stringForColumn:@"password"],[resultSet stringForColumn:@"score"]);    }}

 

         e.修改和刪除

      //參照數(shù)據(jù)的插入和查詢

  

5. 數(shù)據(jù)庫(kù)在項(xiàng)目中使用-單例設(shè)計(jì)模式(交通規(guī)則考試為例)

(1)配置 (進(jìn)行簡(jiǎn)單的配置即可使用)

  導(dǎo)入文件,

  添加二進(jìn)制庫(kù) libsqlite3.dylib,

  包含頭文件#import "FMDatabase.h"

 

(2)使用

  a.創(chuàng)建一個(gè)DatabaseManager的單例(繼承NSObject),里面添加兩個(gè)方法并實(shí)現(xiàn)

//獲取單例對(duì)象+(id)sharedInstance;//獲取第一級(jí)目錄-(NSArray *)firstLevels;

  

#import "DatabaseManager.h"#import "FMDatabase.h"@interface DatabaseManager (){    FMDatabase *_database;}@end@implementation DatabaseManager//獲取單例對(duì)象+(id)sharedInstance{    static DatabaseManager *dc = nil;    if (dc == nil) {        dc = [[[self class]alloc]init];    }    return dc;}//重寫初始化的方法-(id)init{    if (self = [super init]) {        [self openDatabase];    }    return self;}//打開數(shù)據(jù)庫(kù)-(void)openDatabase{    NSString *path = [[NSBundle mainBundle]pathForResource:@"data.sqlite" ofType:nil];    _database = [[FMDatabase alloc]initWithPath:path];    if (!_database.open) {        NSLog(@"打開失敗");    }}//獲取第一級(jí)目錄-(NSArray *)firstLevels{    NSString *sql = @"select *from firstlevel";    FMResultSet *resultSet = [_database executeQuery:sql];    NSMutableArray *marr = [[NSMutableArray alloc]init];    while ([resultSet next]) {                FirstLevelModel *model = [[FirstLevelModel alloc]init];        model.pid = [resultSet stringForColumn:@"pid"];        model.pname = [resultSet stringForColumn:@"pname"];        model.pcount = [resultSet stringForColumn:@"pcount"];                [marr addObject:model];    }    return marr;}

 

  b.實(shí)現(xiàn)  

  c.創(chuàng)建模型model(FirstLevelModel,繼承NSObject)

#import <Foundation/Foundation.h>@interface FirstLevelModel : NSObject@PRoperty (copy,nonatomic) NSString *pid;@property (copy,nonatomic) NSString *pname;@property (copy,nonatomic) NSString *pcount;@end

 

 

    導(dǎo)入單例DatabaseManager的頭文件

#import "ViewController.h"#import "DatabaseManager.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{    UITableView *_tableView ;    NSMutableArray *_dataArray;    }@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        //初始化_dataArray    _dataArray = [[NSMutableArray alloc]init];        //獲取單例    DatabaseManager *manager = [DatabaseManager sharedInstance];        //通過遍歷單例創(chuàng)建Model    for (FirstLevelModel *model  in manager.firstLevels) {        NSLog(@"name = %@",model.pname);        //把遍歷后的數(shù)據(jù)加入到_dataArray中        [_dataArray addObject:model];    }    NSLog(@"%d",manager.firstLevels.count);    [self creatTableView];    }//創(chuàng)建tableView-(void)creatTableView{    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];    _tableView.delegate = self;    _tableView.dataSource = self;    //返回cell的高度    _tableView.rowHeight = 50;    [self.view addSubview:_tableView ];        //給_dataArray的數(shù)據(jù)賦值    _dataArray = [[NSMutableArray alloc]initWithArray: [[DatabaseManager sharedInstance] firstLevels]];    }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSLog(@"%d",[[[DatabaseManager sharedInstance] firstLevels] count]);    return [[[DatabaseManager sharedInstance] firstLevels] count];}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *inde = @"cellId";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inde];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inde];            }        //創(chuàng)建model獲取數(shù)據(jù)并給cell賦值    FirstLevelModel *model = _dataArray[indexPath.row];    cell.textLabel.text = model.pname;    return cell;}//是否可以編輯-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;} 

 

 代碼下載


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿巴嘎旗| 博白县| 贵定县| 酒泉市| 西峡县| 龙胜| 南开区| 来宾市| 石景山区| 永泰县| 诸暨市| 塔河县| 乌海市| 四子王旗| 惠水县| 永年县| 乌鲁木齐县| 兰考县| 鹿泉市| 平乐县| 忻州市| 建水县| 河北省| 前郭尔| 瑞金市| 桃源县| 荥经县| 新源县| 特克斯县| 商河县| 鄂伦春自治旗| 昭平县| 武平县| 德令哈市| 韩城市| 南城县| 进贤县| 陆川县| 开封市| 宜都市| 柘城县|