1 創(chuàng)建文件引入
2 添加到文件中
3 創(chuàng)建一個繼承與NSObject的類
DataBaseHandle.h 文件
#import <Foundation/Foundation.h>
@interface DataBaseHandle : NSObject
//把這個類寫成單例,方便外部使用
+(DataBaseHandle *)shareDataBaseHandle;
- (void)getPath:(NSString *)path;
//打開數(shù)據(jù)庫
-(void)openDB;
//關閉數(shù)據(jù)庫
-(void)closeDB;
//創(chuàng)建表
-(void)creatTable;
//插入數(shù)據(jù)
-(void)insertName:(NSString *)name
gender:(NSString *)gender
age:(NSInteger)age;
//通過uid去更新數(shù)據(jù)
-(void)updateWithUID:(NSInteger)uid;
//通過uid去刪除數(shù)據(jù)
-(void)deleteWithUID:(NSInteger)uid;
//查找所有數(shù)據(jù)
-(void)searchAll;
//根據(jù)名字去找找相關數(shù)據(jù)信息
-(void)searchWithName:(NSString *)name;
@end
DataBaseHandle.m 文件
#import "DataBaseHandle.h"
#import <sqlite3.h>
@interface DataBaseHandle ()
//數(shù)據(jù)庫的存儲路徑
@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路徑下創(chuàng)建數(shù)據(jù)庫文件夾
_dbPath = [document stringByAppendingPathComponent:@"person.sqlite"];
}
return _dbPath;
}
//因為有很多地方會使用到數(shù)據(jù)庫,所以初始化一個數(shù)據(jù)庫的靜態(tài)變量
static sqlite3 *db = nil;
-(void)openDB
{
//第一個參數(shù):代表的是數(shù)據(jù)庫的路徑
//第二個參數(shù):二級指針,代表的是數(shù)據(jù)庫的地址
int result = sqlite3_open(self.dbPath.UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫打開成功");
}else
{
NSLog(@"數(shù)據(jù)庫打開失敗");
}
}
-(void)closeDB
{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫關閉成功");
}else
{
NSLog(@"數(shù)據(jù)庫關閉失敗");
}
}
-(void)creatTable
{
//創(chuàng)建一個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;
//第一個參數(shù):數(shù)據(jù)庫
//第二個參數(shù):sql語句
//第三個參數(shù):語句長度,有正負之分,1:代表往后讀取一個字節(jié),如果為-1的話,那么會自動判斷語句長度
int result = sqlite3_prepare(db, insertStr.UTF8String, -1, &stmt, NULL);
//判斷執(zhí)行結果
if (result == SQLITE_OK) {
//在操作成功的方法里面進行 ? 值的綁定設置
//第一個參數(shù):伴隨指針
//第二個參數(shù): ? 的位置,從1開始.
//第三個參數(shù):表示要插入的值
//第四個參數(shù):-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語句執(zhí)行完畢
//執(zhí)行伴隨指針,根據(jù)伴隨指針的情況判斷是否插入成功
//SQLITE_DONE 代表插入數(shù)據(jù)是否成功
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"插入成功");
}else
{
NSLog(@"插入失敗");
}
}
//一定要記得釋放伴隨指針
sqlite3_finalize(stmt);
}
-(void)updateWithUID:(NSInteger)uid
{
NSString *updateStr = @"update person set name = '隔壁老王' where uid = ?";
//創(chuàng)建伴隨指針
sqlite3_stmt *stmt = nil;
//準備sql函數(shù)
int result = sqlite3_prepare(db, updateStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
//綁定:
sqlite3_bind_int64(stmt, 1, uid);
//執(zhí)行完畢
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"數(shù)據(jù)庫更新成功");
}else
{
NSLog(@"數(shù)據(jù)庫更新失敗");
}
}
sqlite3_finalize(stmt);
}
-(void)deleteWithUID:(NSInteger)uid
{
//準備sql語句
NSString *deleteStr = [NSString stringWithFormat:@"delete from person where uid = %ld",uid];
//sql函數(shù)
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";
//未完待續(xù)
}
- 根據(jù)名字去查找相關數(shù)據(jù)信息
-(void)searchWithName:(NSString *)name
{
//準備sql語句
NSString *searchStr = @"select * from person where name = ?";
//未完待續(xù) ,自己想把
}
@end
ViewController中調用
DataBaseHandle *dataBase = [DataBaseHandle shareDataBaseHandle];
//打開數(shù)據(jù)庫
[dataBase openDB];
//創(chuàng)建表
[dataBase creatTable];
//添加
[dataBase insertName:@"小麗麗" gender:@"女" age:18];
//更新
[dataBase updateWithUID:2];
//刪除
[dataBase deleteWithUID:3];
//關閉
[dataBase closeDB];