常见的数据库有 sql mysql oracle
数据库
以一定的方式储存在一起
能为多个用户共享
具有尽可能小的冗余度
与程序彼此独立的数据集合
数据库模型
层次结构模型
网状结构模型
关系结构模型
sqLite 嵌入式数据库
支持事件,不需要配置,不需要管理
支持大部分sql92;
完整的数据库保存在磁盘上面的一个文件,同一个数据库文件可以支持数据库2T
整个系统少于3万代码,少于250kb内存占用
源代码开发,代码95%有注释,简单易用的app
数据库的操作
插入
insert into …. values
更新
update…set ….where
删除
delete from …..where
查询
select from…. where
//具体的操作命令
#import "MYYViewController.h"
#import <sqlite3.h>
//只是创建表格的回调函数
int createCallBack (void *content,int intValue,char **char1,char **char2){
NSLog(@"%s",__func__);
return 1;
}
@interface MYYViewController ()
@end
@implementation MYYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//打开文件目录
NSString *documentPath = NSSearchPathForDirectoriesInDomains(9, 1, 1)[0];
//创建文件名
NSString *sqlitePath = [documentPath stringByAppendingString:@"/lesson.sqlite"];
NSLog(@"%@",documentPath);
sqlite3 *db = NULL;
//打开创建数据库的函数
int result = sqlite3_open(sqlitePath.UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"打开创建数据库成功");
}
//声明创建表格的sql命令
NSString* createSQL = @"create table Person(p_id integer primary key autoincrement,p_name text,p_age integer,p_sex text);";
char *error = NULL;
//执行命令
result = sqlite3_exec(db, createSQL.UTF8String, createCallBack, @"hehe", &error);
if (result == SQLITE_OK) {
NSLog(@"创建表格成功");
}else{
NSLog(@"创建表格失败的原因是:%s",error);
}
//声明插入对象的命令
NSString *insertSQL = @"insert into Person(p_name,p_age,p_sex) values('孟园园',23,'m');";
result = sqlite3_exec(db, insertSQL.UTF8String, createCallBack, @"insert",&error);
if (result == SQLITE_OK) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败的原因是:%s",error);
}
//更新数据
//声明插入对象的命令
NSString *updateSQL = @"update Person set p_name = 'myy', p_age = 24;";
result = sqlite3_exec(db, updateSQL.UTF8String, createCallBack, @"update",&error);
if (result == SQLITE_OK) {
NSLog(@"更新数据成功");
}else{
NSLog(@"更新数据失败的原因是:%s",error);
}
//删除数据
// NSString *deteleSQl = @"delete from person";
// result = sqlite3_exec(db, deteleSQl.UTF8String, createCallBack, @"detele", &error);
// if (result == SQLITE_OK) {
// NSLog(@"删除数据成功");
// }else{
// NSLog(@"删除数据失败的原因:%s",error);
// }
//查询数据 检索所有的数据
NSString *selectSQL = @"select *from person";
//查询结果额状态对象
sqlite3_stmt *stmt = NULL;
//准备状态对象,
sqlite3_prepare(db, selectSQL.UTF8String, selectSQL.length, &stmt, NULL);
//可变数组
NSMutableArray *rowList= [[NSMutableArray alloc]init];
//循环查询结果的每一行
while (sqlite3_step(stmt) == SQLITE_ROW) {
//得到每一列的值
int p_id = sqlite3_column_int(stmt, 0);
const unsigned char*p_name = sqlite3_column_text(stmt, 1);
int p_age = sqlite3_column_int(stmt, 2);
const unsigned char *p_sex = sqlite3_column_text(stmt, 3);
NSString *pname = [[NSString alloc]initWithFormat:@"%s",p_name];
NSString *psex = [[NSString alloc]initWithFormat:@"%s",p_sex];
id dic = @{@"pid":@(p_id).description,@"page":@(p_age).description,@"pname":pname,@"psex":psex};
[rowList addObject:dic];
}
//摧毁状态结果
sqlite3_finalize(stmt);
NSLog(@"%@",rowList);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "MYYViewController.h"
#import <sqlite3.h>
//只是创建表格的回调函数
int createCallBack (void *content,int intValue,char **char1,char **char2){
NSLog(@"%s",__func__);
return 1;
}
@interface MYYViewController ()
@end
@implementation MYYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//打开文件目录
NSString *documentPath = NSSearchPathForDirectoriesInDomains(9, 1, 1)[0];
//创建文件名
NSString *sqlitePath = [documentPath stringByAppendingString:@"/lesson.sqlite"];
NSLog(@"%@",documentPath);
sqlite3 *db = NULL;
//打开创建数据库的函数
int result = sqlite3_open(sqlitePath.UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"打开创建数据库成功");
}
//声明创建表格的sql命令
NSString* createSQL = @"create table Person(p_id integer primary key autoincrement,p_name text,p_age integer,p_sex text);";
char *error = NULL;
//执行命令
result = sqlite3_exec(db, createSQL.UTF8String, createCallBack, @"hehe", &error);
if (result == SQLITE_OK) {
NSLog(@"创建表格成功");
}else{
NSLog(@"创建表格失败的原因是:%s",error);
}
//声明插入对象的命令
NSString *insertSQL = @"insert into Person(p_name,p_age,p_sex) values('孟园园',23,'m');";
result = sqlite3_exec(db, insertSQL.UTF8String, createCallBack, @"insert",&error);
if (result == SQLITE_OK) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败的原因是:%s",error);
}
//更新数据
//声明插入对象的命令
NSString *updateSQL = @"update Person set p_name = 'myy', p_age = 24;";
result = sqlite3_exec(db, updateSQL.UTF8String, createCallBack, @"update",&error);
if (result == SQLITE_OK) {
NSLog(@"更新数据成功");
}else{
NSLog(@"更新数据失败的原因是:%s",error);
}
//删除数据
// NSString *deteleSQl = @"delete from person";
// result = sqlite3_exec(db, deteleSQl.UTF8String, createCallBack, @"detele", &error);
// if (result == SQLITE_OK) {
// NSLog(@"删除数据成功");
// }else{
// NSLog(@"删除数据失败的原因:%s",error);
// }
//查询数据 检索所有的数据
NSString *selectSQL = @"select *from person";
//查询结果额状态对象
sqlite3_stmt *stmt = NULL;
//准备状态对象,
sqlite3_prepare(db, selectSQL.UTF8String, selectSQL.length, &stmt, NULL);
//可变数组
NSMutableArray *rowList= [[NSMutableArray alloc]init];
//循环查询结果的每一行
while (sqlite3_step(stmt) == SQLITE_ROW) {
//得到每一列的值
int p_id = sqlite3_column_int(stmt, 0);
const unsigned char*p_name = sqlite3_column_text(stmt, 1);
int p_age = sqlite3_column_int(stmt, 2);
const unsigned char *p_sex = sqlite3_column_text(stmt, 3);
NSString *pname = [[NSString alloc]initWithFormat:@"%s",p_name];
NSString *psex = [[NSString alloc]initWithFormat:@"%s",p_sex];
id dic = @{@"pid":@(p_id).description,@"page":@(p_age).description,@"pname":pname,@"psex":psex};
[rowList addObject:dic];
}
//摧毁状态结果
sqlite3_finalize(stmt);
NSLog(@"%@",rowList);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end