1,归档:
单个对象的归档代码:
多个对象归档代码:
数据模型归档方法
2,plist
.plist数据结构:
plist图片.png
Model代码:
@interface Model : NSObject
#pragma mark - <#名称#>
@property (nonatomic , copy ) NSString *title;
@property (nonatomic , strong ) NSArray *rows;
@end
读取数据代码:
//读取数据
NSBundle *bundle = [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"Property" ofType:@"plist"];
NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
//遍历数组
for (NSDictionary *dict in mutableArray) {
Model *model = [[Model alloc]init];
model.title = dict[@"title"];
model.rows = [dict[@"rows"] array];
}
sqlite代码:
//
// ViewController.m
#import "ViewController.h"
#import <sqlite3.h>
#import "Model.h"
@interface ViewController ()
{
sqlite3 *_db;//数据库管理手柄
}
/*
数据库类型
NULL nil
INTEGER 有符号整数
TXET 文本字符串,支持编码格式(UTF-8、UTF-16BE或 UTF-16LE)
REAL real 浮点值
*/
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self open];
//// [self create];
//// [self insert];
//// [self delete];
// [self quarry];
//// [self insert];
// [self closeSqlite];
}
#pragma mark - 打开数据库
-(void)open{
if (_db) {
NSLog(@"数据库打开成功");
return;
}
//路径
NSString *nsPath = [NSString stringWithFormat:@"%@/Documents/Person.db",NSHomeDirectory()];
const char *path = [nsPath UTF8String];
//打开数据库
int ret = sqlite3_open(path, &_db);
if (ret == SQLITE_OK) {
NSLog(@"数据库打开成功了");
}else{
NSLog(@"数据库打开失败了");
}
}
#pragma mark - 创建表格
-(void)create{
//sqlite语句格式:创建表格-Create table If not exists 表格名字英文可以使用下划线 (字段 字段类型,字段1 字段1类型);
const char *sql = "CREATE TABLE IF NOT EXISTS t_Student (id integer PRIMARY KEY AUTOINCREMENT,name text,score real DEFAULT 0,sex text DEFAULT '不明确');";
int result = sqlite3_exec(_db, sql, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"创建表格成功");
}else{
NSLog(@"创建表格失败");
}
}
#pragma mark - 插入信息
-(void)insert{
const char *sql = "INSERT INTO t_Student (name,score,sex) VALUES ('Lucy',5,'女');";
int result = sqlite3_exec(_db, sql, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"数据插入成功");
}else{
NSLog(@"数据插入失败");
}
}
#pragma mark - 删除
-(void)delete{
const char *sql = "DELETE FROM t_Student WHERE score < 10;";
int result = sqlite3_exec(_db, sql, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"数据删除成功");
}else{
NSLog(@"数据删除失败");
}
}
#pragma mark - 查询
-(void)quarry{
const char *sql = "SELECT name,score FROM t_Student;";
sqlite3_stmt *stmt;
//第三个参数,需要传sql语句的长度,-1表示自动计算
int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);//sqlite3_prepare_v2
if (result==SQLITE_OK) {
NSLog(@"查询成功");
while (sqlite3_step(stmt)==SQLITE_ROW) {
const unsigned char *name = sqlite3_column_text(stmt, 0);
double score = sqlite3_column_double(stmt, 1);
NSLog(@"姓名:%s 分数%.2lf",name,score);
}
}else{
NSLog(@"查询失败");
}
}
#pragma mark - 关闭数据库
-(void)closeSqlite{
//数据库关闭
int closeResult = sqlite3_close_v2(_db);//sqlite3_close(_db);
NSLog(@"closeResult : %d",closeResult);
if (closeResult == SQLITE_OK) {
NSLog(@"关闭数据库成功");
}else{
NSLog(@"数据库关闭失败");
}
}
@end
补充:
数据库连接指针:sqlite3*
打开数据库: sqlite3_open(const char *fileName, sqlite3 **ppDb);
创建SQL语句预编译指针:sqlite3_stmt *
预编译SQL语句: sqlite3_prepare _v2(sqlite3 *db,const char *zSql,int nByte,sqlite3_stmt **ppStmt,const char **pzTail);
绑定SQL语句占位符: sqlite3_bind_xxx
执行预编译的SQL语句: sqlite3_step(sqlite3_stmt *)
获取数据表中数据:sqlite3_column_xxx(sqlite3_stmt *,int iCol);
销毁预编译指针: sqlite3_finalize(sqlite3_stmt *pStmt)
关闭数据库: sqlite3_close(sqlite3 *db)
网友评论