iOS開發之用代碼實現資料庫FMDB的操作

來源:互聯網
上載者:User

標籤:

iOS開發之用代碼實現資料庫FMDB的操作1.簡介

需求作用:  如果需要儲存大量的結構較為複雜的資料時候, 使用資料庫, 例如交規考試項目

常用的資料庫:

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

  (2)Oracle 比較複雜, 大企業使用較多

  (3)Mysql資料庫, 網站使用較多

  (4)sqlite:  本機資料庫, 訪問資料足夠快, 直接存取檔案

         足夠簡單, 功能相對其他資料庫軟體不是特別齊全, 足夠用了

          足夠小, 系統不超過1M, 適合在移動端上使用

 

 2.FMDB操作資料庫

(1)配置 

  匯入檔案,

  添加二進位庫 libsqlite3.dylib,

  包含標頭檔#import "FMDatabase.h"

 

3.資料庫在項目中使用-單例設計模式
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        //(1)建立資料庫    [self creatAndInitDatabase];        //(2)建立資料表    [self createTable];        //(3)插入資料    [self insertdata];        //(4)查詢資料    [self queryData];        //(5)修改和刪除資料    //參考:插入資料}-(void)queryData{    //顯示所有人的資訊    NSString *sql = @"select * from StudentInfo";    //FMResultSet 表示查詢後結果集    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"]);    }    }-(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(@"b = %d",b);}-(void)createTable{    NSString *sql=@"create table if not exists StudentInfo(sid integer,username varchar(20),password varchar(20),score varchar(20))";    //executeQuery 用來執行select語句        BOOL b=[_database executeUpdate:sql];    NSLog(@"b=%d",b);}-(void)creatAndInitDatabase{    //ios安全機制 - 沙箱    //(1). 每個應用內容都放在一個沙箱目錄下面    //(2). 每個應用只能修改沙箱目錄下得檔案,其他應用檔案無法修改    //(3). 預設資料夾 Documents,Library,tmp    //開發: 自己建立的檔案放在Documents下面        //確定檔案位置    //當前檔案夾: NSHomeDirectory()//    NSLog(@"home = %@",NSHomeDirectory());//    NSLog(@"%@",[[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSHomeDirectory() error:nil]);            //設定路徑    NSString *path = [NSString stringWithFormat:@"%@/Documents/stuInfo.sqlite",NSHomeDirectory()];    //建立資料庫(如果不存在則建立開啟,如果存在則直接開啟)    _database = [[FMDatabase alloc] initWithPath:path];    if(![_database open])    {        NSLog(@"開啟失敗");        return;    }        NSLog(@"開啟成功");    }

 

4.顯示結果

iOS開發之用代碼實現資料庫FMDB的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.