1 创建文件引入
2 添加到文件中
3 创建一个继承与NSObject的类
DataBaseHandle.h 文件
#import <Foundation/Foundation.h>
@interface DataBaseHandle : NSObject
//把这个类写成单例,方便外部使用
+(DataBaseHandle *)shareDataBaseHandle;
- (void)getPath:(NSString *)path;
//打开数据库
-(void)openDB;
//关闭数据库
-(void)closeDB;
//创建表
-(void)creatTable;
//插入数据
-(void)insertName:(NSString *)name
gender:(NSString *)gender
age:(NSInteger)age;
//通过uid去更新数据
-(void)updateWithUID:(NSInteger)uid;
//通过uid去删除数据
-(void)deleteWithUID:(NSInteger)uid;
//查找所有数据
-(void)searchAll;
//根据名字去找找相关数据信息
-(void)searchWithName:(NSString *)name;
@end
DataBaseHandle.m 文件
#import "DataBaseHandle.h"
#import <sqlite3.h>
@interface DataBaseHandle ()
//数据库的存储路径
@property(nonatomic,strong)NSString *dbPath;
@end
//
static DataBaseHandle *dataBase = nil;
@implementation DataBaseHandle
+(DataBaseHandle *)shareDataBaseHandle
{
if (dataBase == nil) {
dataBase = [[DataBaseHandle alloc] init];
}
return dataBase;
}
-(NSString *)dbPath
{
if (_dbPath == nil) {
//获取decoment路径
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//在document路径下创建数据库文件夹
_dbPath = [document stringByAppendingPathComponent:@"person.sqlite"];
}
return _dbPath;
}
//因为有很多地方会使用到数据库,所以初始化一个数据库的静态变量
static sqlite3 *db = nil;
-(void)openDB
{
//第一个参数:代表的是数据库的路径
//第二个参数:二级指针,代表的是数据库的地址
int result = sqlite3_open(self.dbPath.UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"数据库打开成功");
}else
{
NSLog(@"数据库打开失败");
}
}
-(void)closeDB
{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"数据库关闭成功");
}else
{
NSLog(@"数据库关闭失败");
}
}
-(void)creatTable
{
//创建一个person表, 字段uid integer类型 主键 自增 不能为空,name text类型,gender text类型,age integer类型
NSString *creatStr = @"create table if not exists person(uid integer PRIMARY KEY AUTOINCREMENT not null,name text,gender text,age integer)";
int result = sqlite3_exec(db, creatStr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"建表成功");
}else
{
NSLog(@"建表失败");
}
NSLog(@"_dbPath = %@",self.dbPath);
}
-(void)insertName:(NSString *)name
gender:(NSString *)gender
age:(NSInteger)age
{
//当values不确定的情况下使用 ? 代替
//准备sql语句
NSString *insertStr = @"insert into person(name,gender,age)values(?,?,?)";
//伴随指针
sqlite3_stmt *stmt = nil;
//第一个参数:数据库
//第二个参数:sql语句
//第三个参数:语句长度,有正负之分,1:代表往后读取一个字节,如果为-1的话,那么会自动判断语句长度
int result = sqlite3_prepare(db, insertStr.UTF8String, -1, &stmt, NULL);
//判断执行结果
if (result == SQLITE_OK) {
//在操作成功的方法里面进行 ? 值的绑定设置
//第一个参数:伴随指针
//第二个参数: ? 的位置,从1开始.
//第三个参数:表示要插入的值
//第四个参数:-1
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
sqlite3_bind_text(stmt, 2, gender.UTF8String, -1, NULL);
sqlite3_bind_int64(stmt, 3, age);
//sql语句执行完毕
//执行伴随指针,根据伴随指针的情况判断是否插入成功
//SQLITE_DONE 代表插入数据是否成功
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"插入成功");
}else
{
NSLog(@"插入失败");
}
}
//一定要记得释放伴随指针
sqlite3_finalize(stmt);
}
-(void)updateWithUID:(NSInteger)uid
{
NSString *updateStr = @"update person set name = '隔壁老王' where uid = ?";
//创建伴随指针
sqlite3_stmt *stmt = nil;
//准备sql函数
int result = sqlite3_prepare(db, updateStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
//绑定:
sqlite3_bind_int64(stmt, 1, uid);
//执行完毕
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"数据库更新成功");
}else
{
NSLog(@"数据库更新失败");
}
}
sqlite3_finalize(stmt);
}
-(void)deleteWithUID:(NSInteger)uid
{
//准备sql语句
NSString *deleteStr = [NSString stringWithFormat:@"delete from person where uid = %ld",uid];
//sql函数
int result = sqlite3_exec(db, deleteStr.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else
{
NSLog(@"删除失败");
}
}
-(void)searchAll
{
//准备sql语句
NSString *searchAllStr = @"select * from person";
//未完待续
}
-(void)searchWithName:(NSString *)name
{
//准备sql语句
NSString *searchStr = @"select * from person where name = ?";
//未完待续 ,自己想把
}
@end
ViewController中调用
DataBaseHandle *dataBase = [DataBaseHandle shareDataBaseHandle];
//打开数据库
[dataBase openDB];
//创建表
[dataBase creatTable];
//添加
[dataBase insertName:@"小丽丽" gender:@"女" age:18];
//更新
[dataBase updateWithUID:2];
//删除
[dataBase deleteWithUID:3];
//关闭
[dataBase closeDB];