1. 数据库基本操作封装
DML(增\删\改) DQL(查询) DDL(表格)
- sqlite 数据库的简单实用- 导入sqlite3数据库框架:
1.1 导入框架 libsqlite3.0.tbd
1.3 导入头文件 #import "sqlite3.h"
1. sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs)
是sqlite 的进阶版本(这个有个弊端,当数据库路径不存在不会帮我们创建一个新的)
2. sqlite3_open16(const void *filename, sqlite3 **ppDb) 是sqlite 的utf16编码
3. sqlite3_open(const char *filename, sqlite3 **ppDb) 这个是sqlite 的UTF8 编码,我们使用这个,这个有个特点,当数据库路径不存在是会帮我们创建一个新的数据库并打开
#define DBCachePath NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject
+(BOOL)deal:(NSString *)sql uid:(NSString *)uid{
// 我们使用数据库时引入用户机制
// 如果 用户名为nil ,则 common.db
// 如果 用户名为张三, 则 zhangsan.db
NSString *dbName = @"common.sqlite";
if (uid.length > 0) {
dbName = [NSString stringWithFormat:@"%@.sqlite",uid];
}
NSString *dbPath = [DBCachePath stringByAppendingPathComponent:dbName];
//1. 创建& 打开一个数据库
sqlite3 *db = nil;
int openDbResult = sqlite3_open(dbPath.UTF8String , &db);
if (openDbResult != SQLITE_OK) {
YRLog(@"打开数据库失败");
}
else{
YRLog(@"打开数据库 %@ 成功",dbPath);
}
//2. 执行数据库语句
BOOL exeSqltResult = (sqlite3_exec(db, sql.UTF8String, nil, nil, nil) == SQLITE_OK);
//3. 关闭数据库
sqlite3_close(db);
return exeSqltResult;
}
create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer, score real)
- 将 t_stu表的 id字段 插入 t_stu_temp表的id字段
insert into t_stu_temp(id) select id from t_stu
- 根据 t_stu表的 id 字段 来更新 表t_stu_temp 的name字段的数据
update t_stu_temp set name = (select name from t_stu where t_stu.id = t_stu_temp.id)
删除表t_stu中符合条件的记录
delete from t_stu where name = 'zhangsan'
delete from t_stu where name = 'zhangsan' or num > 30
删除表t_stu中所有的记录
delete from t_stu
drop table if exists t_stu
- 将数据库中名为 t_stu_temp 的表重命名为 t_stu
alter table t_stu_temp rename to t_stu
开启事物 begin transaction
提交事物 commit transaction
回滚事物 rollback transaction
/** 获取 uid 数据库中 ""表名==cls"" 的所有字段(排序后的) NSArray.count == 0 表示该表不存在 */
+(NSArray *)sortedTableColumnNames:(Class)cls uid:(NSString *)uid{
NSMutableArray *columns = [NSMutableArray array];
NSString *tableName = [YRModeTool tableName:cls];
//sql = "CREATE TABLE TestMode (id integer primary key autoincrement, age integer,sut blob,suts blob not null,name text,rowCount3 integer,rowCount integer)"; 这个就是查询到的结果
NSString *querySql = [ NSString stringWithFormat:@"select sql from sqlite_master where type = 'table' and name = '%@'",tableName];
NSArray *resultArr = [YRSqliteTool querySql:querySql uid:uid];
if(resultArr.count == 0) return nil;
NSString *createTableSqlStr = (resultArr[0])[@"sql"];
if(createTableSqlStr.length > 0){
NSArray *arr = [createTableSqlStr componentsSeparatedByString:@"("];
if(arr.count == 2){
NSString *str1 = arr[1];
NSArray *fieldStrs = [str1 componentsSeparatedByString:@","];
for(int i = 0; i < fieldStrs.count; i++){
NSString *fieldStr = [fieldStrs[i] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
NSString *column = [fieldStr componentsSeparatedByString:@" "].firstObject;
[columns addObject:column];
}
}
}
// 排序
[columns sortUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
return [obj1 compare:obj2];
}];
NSString *primaryColumnStr = @"id";
if([columns containsObject:primaryColumnStr]){
[columns removeObject:primaryColumnStr];
}
return columns;
}
2. 数据库-动态建表-基本创建
3.数据库-动态建表-忽略字段
4.数据库-动态更新表-数据迁移
5.数据库-模型操作-保存/更新模型
6.数据库-模型操作-删除模型
7.数据库-模型操作-查询模型
8.数据库-模型操作-优化方案