码迷,mamicode.com
首页 > 移动开发 > 详细

iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找

时间:2016-06-24 20:24:34      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

    本篇主要介绍Sqlite数据库的功能:建表,增加,删除,修改,查找。

  采用封装的方法写的,继承于NSObject。

  需向工程中添加libsqlite3.tbd库。

#import "DataBaseHandle.h"

//引入头文件

#import <sqlite3.h>

@interface DataBaseHandle()

//用来存放数据库的路径

@property (nonatomic,strong) NSString *filePath; 

@end

@implementation DataBaseHandle

//数据库指针

static sqlite3 *DB = nil;

//懒加载

- (NSString *)filePath{

  if (!_filePath){

    //拼接文件路径文件只能以DB和sqlite结尾

    _filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject   stringByAppendingPathComponent:@"Student.sqlite"];

  }

return _filePath;

}

//单例

+(instancetype)sharedDataBase{

  static DataBaseHandle *dataBas = nil;  

  static dispatch_once_t onceToken;

  dispatch_once(&onceToken, ^{

    dataBas = [[DataBaseHandle alloc]init];

  });

return dataBas;

}

//创建表格

- (void)createTable{

  //准备sql语句

  NSString *sqlString = @"create table if not exists Student (name text,age integer)";

  //执行sql语句

  int result = sqlite3_exec(DB, sqlString.UTF8String, NULL, NULL, NULL);

   if (result == SQLITE_OK) {

    NSLog(@"建表成功");

  }else{

    NSLog (@"建表失败");

  }

}

//打开数据库

- (void)openDataBase{

  // 如果数据存在就打开,如果不存在就创建一个再打开

  int result = sqlite3_open(self.filePath.UTF8String, &DB);

  if (result == SQLITE_OK) {

    NSLog(@"数据库打开成功");

  }else{

    NSLog(@"打开失败%d",result);

  }

}

//关闭数据库

- (void) closeDataBase{

  int result = sqlite3_close(DB);

  if (result == SQLITE_OK) {

    NSLog(@"关闭成功");

  }else{

    NSLog(@"关闭失败%d",result);

  }

}

//增

- (void)insertStudentWithName:(NSString *)name age:(NSInteger)age{

  //准备sql语句

  NSString *sqlString = @"insert into Student (name,age) Values (?,?)";

   //创建伴随指针(用来绑定参数,和获取数据)

  sqlite3_stmt *stmt = NULL;

  //预执行

  int result = sqlite3_prepare(DB, sqlString.UTF8String, -1, &stmt, NULL);

  if (result == SQLITE_OK) {

    //参数绑定

    sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);

    sqlite3_bind_int64(stmt, 2, age);

    //开始执行

    if (sqlite3_step(stmt) == SQLITE_DONE) {

      NSLog(@"插入成功");

    }else{

    NSLog(@"插入失败");

    }

 

  }else{

    NSLog(@"语句错误");

  }

//关闭伴随指针

sqlite3_finalize(stmt);

}

//改

- (void)updateWithAge:(NSInteger)age{

  //准备sql语句

  NSString *sqlString = @"update Student set age = 20 where age = ?";

   //创建伴随指针(用来绑定参数,和获取数据)

  sqlite3_stmt *stmt = NULL;

  //预执行

  int result = sqlite3_prepare(DB, sqlString.UTF8String, -1, &stmt, NULL);

  if (result == SQLITE_OK) {

    //参数绑定

    sqlite3_bind_int64(stmt, 1, age);

    //开始执行

    if (sqlite3_step(stmt) == SQLITE_DONE) {

      NSLog(@"更新成功");

    }else{

      NSLog(@"更新失败");

    }

  }else{

    NSLog(@"语句错误");

  }

//关闭伴随指针

sqlite3_finalize(stmt);

}

//查

- (void)selectWithName:(NSString *)name{

  //准备sql语句

  NSString *sqlString = @"select name,age from Student where name = ?";

  //创建伴随指针

  sqlite3_stmt *stmt = nil;

  //预执行

  int result = sqlite3_prepare(DB, sqlString.UTF8String, -1, &stmt, NULL);

  if (result == SQLITE_OK) {

    //参数绑定

    sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);

    //开始执行

    while (sqlite3_step(stmt) == SQLITE_ROW){

    //从伴随指针获取数据

    NSString *readName = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];

    NSInteger readAge = sqlite3_column_int64(stmt , 1);

    NSLog(@"%@,%ld",readName,readAge);

    }

  }else{

  NSLog(@"语句错误");

  }

//关闭伴随指针

sqlite3_finalize(stmt);

}

//删

- (void)deleteStudentWithAge:(NSInteger)age{

  //准备sql语句

  NSString *sqlString = [NSString stringWithFormat:@"delete from Student where age = %ld",age];

  //执行

  int result = sqlite3_exec(DB, sqlString.UTF8String, NULL, NULL, NULL);

  if (result == SQLITE_OK) {

    NSLog(@"删除成功");

  }else{

    NSLog(@"删除失败%d",result);

  }

}

@end

iOS: Sqlite数据库的功能:建表,增加,删除,修改,查找

标签:

原文地址:http://www.cnblogs.com/zsl9855soley/p/5615133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!