iOS数据库Sqlite

引入libsqlite3.0.dylib

#import <UIKit/UIKit.h>

//引入头文件

#import "FMDatabase.h"

#import "Student.h"

@interface RootViewController : UIViewController

{

   FMDatabase *db;

}

@end

//****************************************************************//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

   // 获取douPath路径

   NSString *douPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

   // 获取documens文件下的dbPath路径

   NSString *dbPath = [douPath stringByAppendingPathComponent: @"students.sqlite"];

   // 建库

   db = [FMDatabase databaseWithPath: dbPath];

   NSLog(@"%@",NSHomeDirectory());

   self.navigationItem.title = @"数据库";

   UIButton *creatButton = [UIButton buttonWithType:UIButtonTypeCustom];

   creatButton.frame = CGRectMake(50, 10, 100, 40);

   [creatButton setTitle:@"建表" forState:UIControlStateNormal];

   creatButton.backgroundColor = [UIColor blackColor];

   creatButton.showsTouchWhenHighlighted = YES;

   [self.view addSubview: creatButton];

   [creatButton addTarget:self action:@selector(createTable) forControlEvents:UIControlEventTouchUpInside];

   UIButton *insertButton = [UIButton buttonWithType:UIButtonTypeCustom];

   insertButton.frame = CGRectMake(50, 70, 100, 40);

   [insertButton setTitle:@"插入" forState:UIControlStateNormal];

   insertButton.backgroundColor = [UIColor blackColor];

   insertButton.showsTouchWhenHighlighted = YES;

   [self.view addSubview: insertButton];

   [insertButton addTarget:self action:@selector(insertStudent) forControlEvents:UIControlEventTouchUpInside];

   UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];

   updateButton.frame = CGRectMake(50, 130, 100, 40);

   [updateButton setTitle:@"更新" forState:UIControlStateNormal];

   updateButton.backgroundColor = [UIColor blackColor];

   updateButton.showsTouchWhenHighlighted = YES;

   [self.view addSubview: updateButton];

   [updateButton addTarget:self action:@selector(updateStudent) forControlEvents:UIControlEventTouchUpInside];

   UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];

   selectButton.frame = CGRectMake(50, 190, 100, 40);

   [selectButton setTitle:@"查询" forState:UIControlStateNormal];

   selectButton.backgroundColor = [UIColor blackColor];

   selectButton.showsTouchWhenHighlighted = YES;

   [self.view addSubview: selectButton];

   [selectButton addTarget:self action:@selector(selectStudent) forControlEvents:UIControlEventTouchUpInside];

   UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];

   deleteButton.frame = CGRectMake(50, 250, 100, 40);

   [deleteButton setTitle:@"删除" forState:UIControlStateNormal];

   deleteButton.backgroundColor = [UIColor blackColor];

   deleteButton.showsTouchWhenHighlighted = YES;

   [self.view addSubview: deleteButton];

   [deleteButton addTarget:self action:@selector(deleteStudent) forControlEvents:UIControlEventTouchUpInside];

    // Do any additional setup after loading the view.

}

// 建表操作

- (void)createTable

{

   // 打开数据库

   if ([db open]) {

      // 建表的语句

      // 表名叫student 表中有三个字段(姓名 年龄 爱好)

      // 数据库操作  创建语句  执行语句

      NSString *createTableSql = [NSString stringWithFormat:@"create table if not exists student(id integer primary key autoincrement,name text,age integer,hobby text)"];

      // 执行语句executeUpdate

      BOOL result = [db executeUpdate: createTableSql];

      if (result) {

         NSLog(@"建表成功!");

      } else {

         NSLog(@"建表失败!");

      }

   }

   // 关闭数据库

   [db close];

}

// 向表中插入数据

- (void)insertStudent

{

   Student *stu1 = [[Student alloc] init];

   stu1.name = @"东哥";

   stu1.age = 15;

   stu1.hobby = @"三国演义";

   if ([db open]) {

      //创建插入语句

      NSString *insertSql = [NSString stringWithFormat:@"insert into student (name,age,hobby) values ('%@',%ld,'%@')",stu1.name,stu1.age,stu1.hobby];

      //执行语句

      BOOL result = [db executeUpdate:insertSql];

      if (result) {

         NSLog(@"插入成功!");

      } else {

         NSLog(@"插入失败!");

      }

   }

   //关闭数据库

   [db close];

}

//更新学生的方法

- (void)updateStudent

{

   if ([db open]) {

      //创建数据库更新语句

      NSString *updateSql = [NSString stringWithFormat:@"update student set hobby = '小文文' where hobby = '罗玉凤'"];

      //执行语句

      BOOL result = [db executeUpdate: updateSql];

      if (result) {

         NSLog(@"更新成功!");

      } else {

         NSLog(@"更行失败!");

      }

   }

   [db close];

}

//删除学生数据  数据库删除语句

- (void)deleteStudent

{

   if ([db open]) {

      NSString *deleteSql = [NSString stringWithFormat:@"delete from student where name = '班长'"];

      //执行语句

      BOOL result = [db executeUpdate: deleteSql];

      if (result) {

         NSLog(@"删除成功!");

      } else {

         NSLog(@"删除失败!");

      }

   }

   [db close];

}

//查询数据库方法

- (void)selectStudent

{

   NSMutableArray *array = [NSMutableArray array];

   if ([db open]) {

      //创建数据库查询语句

      NSString *selectSql = [NSString stringWithFormat:@"select * from student"];

      //执行查询

      FMResultSet *set = [db executeQuery: selectSql];

      while ([set next]) {

         Student *stu = [[Student alloc] init];

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

         stu.age = [set intForColumn:@"age"];

         stu.hobby = [set stringForColumn:@"hobby"];

         [array addObject: stu];

      }

      NSLog(@"%@",array);

   }

   [db close];

}

//**********************************************************************//

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property(nonatomic,strong)NSString *name;

@property(nonatomic,assign)NSInteger age;

@property(nonatomic,strong)NSString *hobby;

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值