qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          數據庫類庫FMDB

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

           3.批量的創建表和插入數據
          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];
            當然也可以通過數組的方式插入數據
            NSArray *stuentInfo = [NSArray arrayWithObjects:@"大傻瓜",@"213",@"123456@qq.com", nil nil];
            [dataBase executeUpdate:@"insert into student (name, password, email) values (?,?,?)" withArgumentsInArray:stuentInfo];
            4.匹配的方式插入數據
            NSString *sqlInsert = @"INSERT INTO student (name, password, email) values (?,?,?)";
            BOOL flag = [dataBase executeUpdate:sqlInsert, @"大寶貝", @"123", @"654321@qq.com"];
            NSLog(@"%d", flag);
            5.1.查詢所有數據
            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.執行刪除操作
            NSString *sql = @"delete from student where name = ?";
            [dataBase executeUpdate:sql,@"寶貝"];
            執行更新操作的步驟和刪除操作一樣,只不過sql語句不同而已
            重要注意事項是:1 使用完數據庫后記得關閉
            2 查詢結果完場后result也得記得關閉

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

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永昌县| 乌拉特后旗| 长春市| 大宁县| 偏关县| 宁明县| 福建省| 保山市| 沁水县| 桂阳县| 富平县| 阿巴嘎旗| 涪陵区| 宁河县| 沁水县| 承德县| 偏关县| 宜川县| 屯门区| 军事| 武平县| 宁波市| 盐池县| 岑溪市| 无为县| 交城县| 鹤山市| 嘉定区| 盐池县| 郓城县| 赞皇县| 丰城市| 兴文县| 方山县| 修武县| 温宿县| 洪泽县| 福建省| 偃师市| 烟台市| 武陟县|