iOS 資料庫FMDN 資料庫的增 刪 改 查 的 的基本操作

來源:互聯網
上載者:User

標籤:基礎   sql   

FMDB的 (https://github.com/ccgus/fmdb) 話不多說首先將這個 庫加進來,在將加進來,OK  準備工作就緒上代碼。

#import "DDViewController.h"

#import "FMDB.h"


@interface DDViewController ()

{

    FMDatabase *db;

}


@end


@implementation DDViewController

/**

 *  要先做要有個位置去存放資料庫  ios下Document路徑,Document為ios中可讀寫的檔案夾並且要建立一個資料庫的列表

 */

- (void)viewDidLoad

{

    [superviewDidLoad];

//    db = [FMDatabase new];

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

   NSString *documentDirectory = [paths objectAtIndex:0];

    //dbPath:資料庫路徑,在Document中。

   NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];

    //建立資料庫執行個體 db 這裡說明下:如果路徑中不存在"Test.db"的檔案,sqlite會自動建立"Test.db"

   db= [FMDatabasedatabaseWithPath:dbPath] ;

   if (![db open]) {

        NSLog(@"Could not open db.");

       return ;

    }

    //為資料庫設定緩衝,提高查詢效率

    [dbsetShouldCacheStatements:YES];

// 1.建立一個表 表的名字 為User

    [selfcreatTable];

    //2.向表中添加資料

    [selfinsertData:@"草帽" age:23];

    //3.查看結果

    [selfResultSet];


}

//建立一個表

-(void)creatTable

{

//建立表的時候 最好判斷下自己的之前的工作有麼有做好

   if (!db) {

       NSLog(@"資料庫不存在");

    }

   if (![db open]) {

        [dbopen];

    }

    //判斷是否存在這張名字為User的表如果沒有 建立

   if (![db tableExists:@"User"]) {

        //建立一個名為User的表,有兩個欄位分別為string類型的Name,integer類型的 Age

        [dbexecuteUpdate:@"CREATE TABLE User (Name text,Age integer)"];

//        [self insertData:@"草帽" age:24];


    }

}

//插入資料

-(void)insertData:(NSString *)name age:(int)age

{

    //插入資料使用OC中的類型 text對應為NSString integer對應為int的整型

    [dbexecuteUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",name,[NSNumbernumberWithInt:age]];


}

//查詢結果

-(void)ResultSet

{

    //返回資料庫中第一條滿足條件的結果

    NSString *aa=[dbstringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"20"];

    NSLog(@"aa is %@",aa);

    

    FMResultSet *rs=[dbexecuteQuery:@"SELECT * FROM User"];

//    rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"23"];

   while ([rs next]){

        NSLog(@"%@ %@",[rsstringForColumn:@"Name"],[rsstringForColumn:@"Age"]);

    }

    [rsclose];


}

//修改表內容

-(void)Modify

{

    [dbexecuteUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"李四",@"張三"];

}

- (void)didReceiveMemoryWarning


{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


iOS 資料庫FMDN 資料庫的增 刪 改 查 的 的基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.