<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經(jīng)轉移至github,大家請訪問 http://qaseven.github.io/

    數(shù)據(jù)庫類庫FMDB

    前言
      SQLite (http://www.sqlite.org/docs.html) 是一個輕量級的關系數(shù)據(jù)庫。iOS SDK很早就支持了SQLite,在使用時,只需要加入 libsqlite3.dylib 依賴以及引入 sqlite3.h 頭文件即可。但是,原生的SQLite API在使用上相當不友好,在使用時,非常不便。于是,開源社區(qū)中就出現(xiàn)了一系列將SQLite API進行封裝的庫,而FMDB (https://github.com/tryingx/fmdb-master) 則是開源社區(qū)中的優(yōu)秀者。
      第一步 引入:sqlite3的類庫
      第二步 引入:FMDB的類庫
      主要包括以下幾方面
      引入文件結束以后就可以使用了,使用很簡單
      第一步 建立一個數(shù)據(jù)庫類
    1 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 獲取document文件的路徑
    2 /**
    3  *  強調一點就是對于數(shù)據(jù)庫的名:我們可以用"" 或用 NULL
    4  *
    5  *  An empty string (@""). An empty database is created at a temporary location. This database is deleted with the FMDatabase connection is closed.
    6  也就是說如果我們使用(@""),會創(chuàng)建一個臨時數(shù)據(jù)庫,當我們數(shù)據(jù)庫關閉后會自動刪除
    7  NULL. An in-memory database is created. This database will be destroyed with the FMDatabase connection is closed.
    8  在內存中給你創(chuàng)建一個數(shù)據(jù)庫
    9  */
    10 NSString *dbPath = [docPath stringByAppendingPathComponent:@"student.sqlite"];
    11 NSLog(@"%@", dbPath); // 拼接字符串
    12 FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath];
    13 [dataBase open];
    14 // 用來判斷數(shù)據(jù)庫打開是否成功
    15 if (![dataBase open]) {
    16     NSLog(@"error");
    17 }
      第二步 就是執(zhí)行一些常用的操作
      常用操作:
      1.創(chuàng)建一個表
      NSString *sql = @"CREATE TABLE student(_id Integer primary key, name text, password text, email text)";
      BOOL isCreateTable = [dataBase executeStatements:sql];
      NSLog(@"%d", isCreateTable );
      2.插入數(shù)據(jù)
      NSString *sqlInsert = @"INSERT INTO student (name, password, email) values ('寶貝', '1232', '3343243')";
      BOOL isInsertOK = [dataBase executeStatements:sqlInsert];
      NSLog(@"%d", isInsertOK );

     3.批量的創(chuàng)建表和插入數(shù)據(jù)
    NSString *sql1 = @"create table bulktest1 (id integer primary key autoincrement, x text);"
    "create table bulktest2 (id integer primary key autoincrement, y text);"
    "create table bulktest3 (id integer primary key autoincrement, z text);"
    "insert into bulktest1 (x) values ('XXX');"
    "insert into bulktest2 (y) values ('YYY');"
    "insert into bulktest3 (z) values ('ZZZ');";
    [dataBase executeStatements:sql1];
      當然也可以通過數(shù)組的方式插入數(shù)據(jù)
      NSArray *stuentInfo = [NSArray arrayWithObjects:@"大傻瓜",@"213",@"123456@qq.com", nil nil];
      [dataBase executeUpdate:@"insert into student (name, password, email) values (?,?,?)" withArgumentsInArray:stuentInfo];
      4.匹配的方式插入數(shù)據(jù)
      NSString *sqlInsert = @"INSERT INTO student (name, password, email) values (?,?,?)";
      BOOL flag = [dataBase executeUpdate:sqlInsert, @"大寶貝", @"123", @"654321@qq.com"];
      NSLog(@"%d", flag);
      5.1.查詢所有數(shù)據(jù)
      FMResultSet *result = [dataBase executeQuery:@"SELECT * FROM student"];
      [self showInfo:result];
      [result close];
    - (void)showInfo:(FMResultSet *)result
    {
    while ([result next]) {
    int _id = [result intForColumnIndex:0];
    NSLog(@"id : %d", _id);
    NSString *name = [result stringForColumnIndex:1];
    NSLog(@"name : %@", name);
    NSString *password = [result stringForColumnIndex:2];
    NSLog(@"password : %@", password);
    NSString *email = [result stringForColumnIndex:3];
    NSLog(@"email : %@", email);
    }
    }
      5.2.查詢指定信息
      NSString *sqlInsert = @"SELECT * from  student where name = %@";
      FMResultSet *result = [dataBase executeQueryWithFormat:sqlInsert, @"寶貝"];
      NSLog(@"%@", result);
      [self showInfo:result];
      6.執(zhí)行刪除操作
      NSString *sql = @"delete from student where name = ?";
      [dataBase executeUpdate:sql,@"寶貝"];
      執(zhí)行更新操作的步驟和刪除操作一樣,只不過sql語句不同而已
      重要注意事項是:1 使用完數(shù)據(jù)庫后記得關閉
      2 查詢結果完場后result也得記得關閉

    posted on 2014-05-14 10:12 順其自然EVO 閱讀(199) 評論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫

    <2014年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統(tǒng)計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人免费无码大片a毛片软件| 最新亚洲精品国偷自产在线| 免费a级毛片无码a∨性按摩| 91嫩草免费国产永久入口| 中文字幕成人免费高清在线视频 | 99久久99这里只有免费的精品| 国产成人精品日本亚洲网址| 亚洲AV无码精品色午夜果冻不卡| 亚洲成a人在线看天堂无码| 我想看一级毛片免费的| 久草视频在线免费| 99re在线视频免费观看| 免费一级毛片无毒不卡| 中国黄色免费网站| 一区二区三区免费视频网站 | 国产片免费在线观看| 国产免费久久精品99re丫y| 亚洲成人免费网站| 久久精品人成免费| 午夜无码A级毛片免费视频| a级毛片免费全部播放| 国产免费播放一区二区| 72pao国产成视频永久免费| 免费看一级一级人妻片| 免费夜色污私人影院网站电影 | 亚洲精品国产va在线观看蜜芽| 国产精品美女自在线观看免费| 全免费一级午夜毛片| 女人与禽交视频免费看| 久久天天躁狠狠躁夜夜免费观看| 国产成人精品免费视频大| 99久久精品日本一区二区免费| 亚洲w码欧洲s码免费| 69堂人成无码免费视频果冻传媒 | 亚洲精品视频专区| 亚洲性色成人av天堂| 亚洲香蕉在线观看| 亚洲国产精品成人AV在线| 亚洲国产无线乱码在线观看| 国产精品亚洲一区二区三区| 日本在线观看免费高清|