• FMDatabase


    //

    //  ZBMainViewController.m

    //  FMDBDemo

    //

    //  Created by 张先森 on 14/11/27.

    //  Copyright (c) 2014年 zhibin. All rights reserved.

    //

    #import "ZBMainViewController.h"

    #import "FMDatabase.h"

    @interface ZBMainViewController ()

    - (IBAction)Create:(id)sender;

    - (IBAction)Insert:(id)sender;

    - (IBAction)Edit:(id)sender;

    - (IBAction)Del:(id)sender;

    - (IBAction)Select:(id)sender;

    @end

       FMDatabase *db;

        NSString *TABLENAME=@"zbtest";

    @implementation ZBMainViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        [self InitDataBase];

        

    }

    -(void)InitDataBase{

        

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

        

        NSString *documentDirectory = [paths objectAtIndex:0];

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

        

        db = [FMDatabase databaseWithPath:dbPath];

    }

    - (IBAction)Create:(id)sender {

        

        if ([db open]) {

       

            NSString *createtable= [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,@"ID",@"NAME",@"AGE",@"ADDRESS"];

            BOOL result=[db executeUpdate:createtable];

            

            if (!result) {

                NSLog(@"error when creating db table");

            } else {

                NSLog(@"success to creating db table");

            }

            [db close];

            

        }

        

        

        

        

    }

    - (IBAction)Insert:(id)sender {

        

        if ([db open]) {

            

            NSString *insertdata=[NSString stringWithFormat:

                                  @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",

                                  TABLENAME, @"NAME", @"AGE", @"ADDRESS", @"张三", @"13", @"济南"];

            

            

            BOOL result=[db executeUpdate:insertdata];

            if (!result) {

                NSLog(@"error when insert db table");

            } else {

                NSLog(@"success to insert db table");

            }

            [db close];

            

            

        }

        

    }

    - (IBAction)Edit:(id)sender {

        

        if ([db open]) {

        

            BOOL res = [db executeUpdate:@"UPDATE zbtest SET AGE=? WHERE AGE=?",@"15",@"13"];

            

            if (!res) {

                NSLog(@"error when update db table");

            } else {

                NSLog(@"success to update db table");

            }

            [db close];

            

        }

        

        

        

        

    }

    - (IBAction)Del:(id)sender {

        

        

        if ([db open]) {

            

            NSString *deleteSql = [NSString stringWithFormat:

                                   @"delete from %@ where %@ = '%@'",

                                   TABLENAME, @"NAME", @"张三"];

            BOOL res = [db executeUpdate:deleteSql];

            

            if (!res) {

                NSLog(@"error when delete db table");

            } else {

                NSLog(@"success to delete db table");

            }

            [db close];

            

        }

    }

    - (IBAction)Select:(id)sender {

        

        if ([db open]) {

            NSString * sql = [NSString stringWithFormat:

                              @"SELECT * FROM %@",TABLENAME];

            FMResultSet * rs = [db executeQuery:sql];

            while ([rs next]) {

                int Id = [rs intForColumn:@"ID"];

                NSString * name = [rs stringForColumn:@"NAME"];

                NSString * age = [rs stringForColumn:@"AGE"];

                NSString * address = [rs stringForColumn:@"ADDRESS"];

                NSLog(@"id = %d, name = %@, age = %@  address = %@", Id, name, age, address);

            }

            [db close];

        }

        

    }

    @end

  • 相关阅读:
    Python学习第15天_模块
    Python学习第14天_文件读取写入
    Python学习第13天_练习(图书馆的创建)
    Python学习第12天_类
    Python学习第11天_参数
    Python学习第10天_函数
    Python学习第九天_模块的应用
    Android Bluetooth HIDL服务分析
    Mac下CLion配置Google GTest小结
    MacOS通过homebrew安装老版本的软件
  • 原文地址:https://www.cnblogs.com/zhibin/p/4127252.html
Copyright © 2020-2023  润新知