一、添加联系人(WriteToFile版)
HW_Add(AlertView)AndEdit_DataStore0412
二、数据库SQLite3
(一)准备工作
1.导入SQLLite3静态库
2.导入头文件
#import
3.arc下需要 -fno-objc-arc
(二)操作数据库
基本步骤:
1.数据库文件(db.sqlite)必须存在
2.打开数据库(系统自动判断 db.sqlite 是否存在,若不存在,先创建,再打开)
3.create table
4.insert/update/delete/select
打开数据库:
1.获取数据库文件路径(“const char ”类型)
2.调用sqlite3_open*(cPath, &_db);
创建表/插入/修改/删除:
1.打开数据库
2.准备SQL语句(“const char ”类型)
3.调用sqlite3_exec*(_db, cStr, NULL, NULL, nil);
4.关闭数据库
查询:
1.打开数据库
2.准备缓冲区
2.1 准备SQL语句(“const char ”类型)
2.2 调用sqlite3_prepare(db, cStr, -1, &stmt, nil);
3.查询。调用sqlite3step*(stmt)。
4.释放缓冲区,关闭数据库。
提示:
句柄:
数据库句柄,用来操作数据库文件
断言:
源码:
#pragma mark - **************** 获取路径
- (NSString *)getFilePath
{
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",path);
return [path stringByAppendingPathComponent:@"teacher.sqlite"];
}
#pragma mark - **************** 打开数据库
- (BOOL)openDatabase
{
//打开数据库
//参数1:数据库文件路径
NSString *path = [self getFilePath];
const char *cPath = [path UTF8String];
//参数2:句柄(指针)
int isSuccess = sqlite3_open(cPath, &_db);
//SQLITE_OK == 0
if (isSuccess == SQLITE_OK)
{
//数据库打开成功
return YES;
}
else
{
return NO;
}
}
#pragma mark - **************** 创建表
- (IBAction)createTableClick:(UIButton *)sender
{
if ([self openDatabase] == YES)
{
NSLog(@"数据库打开成功");
NSString *str = @"create table teacher (teacher_id integer primary key autoincrement not null,name text,age integer)";
const char *cStr = [str UTF8String];
int isSuccess = sqlite3_exec(_db, cStr, NULL, NULL, nil);
if (isSuccess == SQLITE_OK)
{
NSLog(@"表创建成功");
}
else
{
NSLog(@"表创建失败");
}
}
//关闭数据库
sqlite3_close(_db);
}
#pragma mark - **************** 插入数据
- (IBAction)insertClick:(UIButton *)sender
{
if ([self openDatabase] == YES)
{
NSString *str = [NSString stringWithFormat:@"insert into teacher(name,age)values('%@',%d)",_nameTf.text,[_ageTf.text integerValue]];
const char *cStr = [str UTF8String];
int isSuccess = sqlite3_exec(_db, cStr, NULL, NULL, nil);
if (isSuccess == SQLITE_OK)
{
NSLog(@"插入成功");
}
else
{
NSLog(@"插入失败");
}
}
//关闭数据库
sqlite3_close(_db);
}
#pragma mark - **************** 修改
- (IBAction)updateClick:(UIButton *)sender
{
if ([self openDatabase] == YES)
{
NSString *str = [NSString stringWithFormat:@"update teacher set name = '%@' where teacher_id = 2",_nameTf.text];
sqlite3_exec(_db, [str UTF8String], NULL, NULL, nil);
}
sqlite3_close(_db);
}
- (IBAction)deleteClick:(UIButton *)sender
{
if ([self openDatabase] == YES)
{
NSString *str = @"delete from teacher where teacher_id = 3";
sqlite3_exec(_db, [str UTF8String], NULL, NULL, nil);
}
sqlite3_close(_db);
}
#pragma mark - **************** 查询
- (IBAction)selectClick:(UIButton *)sender
{
if ([self openDatabase] == YES)
{
//1.准备缓冲区
//sqlite3_prepare方法:准备缓冲区
//将数据放入缓冲区
//从缓冲区中逐条查询数据,直到缓冲区中数据被读取完毕,查询结束
//参数1:句柄
//参数2:sql语句
//参数3:sql 语句的长度。-1表示系统帮你计算
//参数4:缓冲区,系统提供
//参数5:一般为nil
NSString *str = @"select name,age from teacher where teacher_id = 1";
const char *cStr = [str UTF8String];
sqlite3_stmt *stmt = nil;//缓冲区
int isSuccess = sqlite3_prepare(_db, cStr, -1, &stmt, nil);
if (isSuccess == SQLITE_OK)
{
//2.查询
//缓冲区准备就绪,从中读取数据(逐条查询)
//sqlite3_step(stmt):表示执行单步查询数据,每次从缓冲区只读取一条记录,
//若返回值为SQLITE_ROW,表示has another row ready,反之,查询结束
//SQLITE_ROW:sqlite3_step() has another row ready
while (sqlite3_step(stmt) == SQLITE_ROW)
{
//参数2:sql语句中 字段索引
const char *name = (const char *)sqlite3_column_text(stmt, 0);
NSString *nameStr = [NSString stringWithUTF8String:name];
int age = sqlite3_column_int(stmt, 1);
NSLog(@"%@%d",nameStr,age);
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(_db);
}