zoukankan      html  css  js  c++  java
  • 数据库

     FMDB 是基于 SQLite 封装的 面向对对象(OC) 的API.

    • FMDB是iOS平台的SQLite数据库框架
    • FMDB以OC的方式封装了SQLite的C语言API

    FMDB 需要libsqlite3.0 系统库的支持

    /////////////////////////  实现代码   ////////////////////////////////////////////////

    #import "PPFMDBTool.h"

    #import "PPShop.h"

    #import "FMDB.h"

    @implementation PPFMDBTool

    SingletonM(fmdbTool)

    static FMDatabase *_db;

    + (void)initialize

    {

        // 1. 打开数据库

        _db = [FMDatabase databaseWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  lastObject] stringByAppendingPathComponent:@"shop.sqilte"]];

        

        BOOL isSuc = [_db open];

        

        if (isSuc) {

            // 2. 创建表

            [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, name text NOT NULL, price real);"];

        }else{

            LogGreen(@"打开数据库 -- 失败");

        }

    }

    + (void)insertShop:(PPShop *)shop

    {

        [_db executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %f);", shop.name, shop.price];

    }

    + (NSArray *)shops

    {

        // 查询 - 得到结果集

        FMResultSet *set = [_db executeQuery:@"SELECT * FROM t_shop;"];

        

        NSMutableArray *shops = [NSMutableArray array];

        // 不断往下取数据

        while ([set next]) {

            // 获得当前所指向的数据

            PPShop *shop = [[PPShop alloc] init];

            shop.name = [set stringForColumn:@"name"];

            shop.price = [set doubleForColumn:@"price"];

            [shops addObject:shop];

        }

        return shops;

    }

    + (void)deleteShop{

        // 删除 价格低于500 的记录

        [_db executeUpdate:@"DELETE FROM t_shop WHERE price < 5000;"];

    }

  • 相关阅读:
    HDU 1069 Monkey and Banana
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    Gym100923H Por Costel and the Match
    Codeforces 682C Alyona and the Tree
    Codeforces 449B Jzzhu and Cities
    Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes
    Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
    Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板
    快乐数问题
  • 原文地址:https://www.cnblogs.com/guangleijia/p/4846617.html
Copyright © 2011-2022 走看看