SQLite常用语句
// 错误存储路径
- (NSString *)dataFilePath{
NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *db_absoluteString = [dbPath stringByAppendingPathComponent:@"personal.db"]; // 找到personal.db数据库
NSLog(@"db_absoluteString: %@",db_absoluteString);
return db_absoluteString; // 返回数据库的路径
}
//插入数据
- (NSString *)insertDBFilePath{
NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *db_absoluteString = [dbPath stringByAppendingPathComponent:@"cmread.sqlite"];
// NSLog(@"db_absoluteString: %@",db_absoluteString);
return db_absoluteString;
}
//获得所有个人信息
- (NSMutableArray *)getStoredPersons{
// 打开数据库
// UTFString格式
// 数据文件路径
if(sqlite3_open([[self dataFilePath] UTF8String],&database) !=SQLITE_OK){
sqlite3_close(database); // 关闭数据库
NSLog(@"open database failed!");
return nil;
}
NSMutableArray *array = [[NSMutableArray alloc] init];
NSString *query = [[NSString alloc] initWithFormat: @"select zaccountname,zlastlogintime,zlogincount from zLoginRecord order by zlastlogintime desc"];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, [query UTF8String],-1,&statement,nil) == SQLITE_OK){
while(sqlite3_step(statement) == SQLITE_ROW){
CMLoginRecord *loginRecord = [[CMLoginRecord alloc] init];
char *loginRecordKey = (char *)sqlite3_column_text(statement, 0);
if (loginRecordKey == NULL) {
loginRecord.accountname = nil;
}else {
NSString *_loginRecord = [[NSString alloc] initWithUTF8String:loginRecordKey];
loginRecord.accountname = _loginRecord;
}
char *lastlogintimeKey = (char *)sqlite3_column_text(statement, 1);
if (lastlogintimeKey == NULL) {
loginRecord.lastlogintime = nil;
}else {
NSString *_lastlogintime = [[NSString alloc] initWithUTF8String:lastlogintimeKey];
loginRecord.lastlogintime = _lastlogintime;
}
int logincount = sqlite3_column_int(statement, 2);
loginRecord.logincount = @(logincount);
[array addObject:loginRecord];
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return array;
}
// 插入个人信息
- (NSInteger)insertPersonl:(NSMutableArray *)array{
BOOL isInsertSuccess = NO;
// 打不开的话,就关闭
if(sqlite3_open([[self insertDBFilePath] UTF8String],&database) !=SQLITE_OK){
sqlite3_close(database);
return 1;
}
char *insert = "insert into zAccount(zaccountname,zlastlogintime,zlogincount) values(?,?,?);";// 要记住分号
for (CMLoginRecord *loginRecord in array) {
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(database,insert,-1,&stmt,nil) == SQLITE_OK){
sqlite3_bind_text(stmt, 1, [loginRecord.accountname UTF8String],-1,NULL);
sqlite3_bind_text(stmt, 2, [loginRecord.lastlogintime UTF8String],-1,NULL);
sqlite3_bind_int(stmt, 3, [loginRecord.logincount intValue]);
}
if(sqlite3_step(stmt)!= SQLITE_DONE){
NSLog(@"Error insert data into zLoginRecord error !");
}else{
//删除原来的数据库
isInsertSuccess = YES;
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
if (isInsertSuccess) {
NSString *path = [self dataFilePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
[fileManager removeItemAtPath:path error:nil];
}
NSLog(@"删除原来的数据库");
}
return 0;
}
SQLite常用语句的更多相关文章
- Sqlite常用sql语句
sqlite常用sql语句 --返回UTC时间 select CURRENT_TIMESTAMP; --返回本地时间 select datetime(CURRENT_TIMESTAMP,'localt ...
- iOS开发数据库篇—SQLite常用的函数
iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename, // 数据库的文件路径 sqlite3 * ...
- ORM数据库框架 SQLite 常用数据库框架比较 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Sqlite 常用函数推荐
Sqlite 常用函数 1 .打开数据库: 说明:打开一个数据库,文件名不一定要存在,如果此文件不存在, sqlite 会自动创建.第一个参数指文件名,第二个参数则是定义的 sqlite3 ** 结构 ...
- sqlite常用的命令-增删改查
一.查看版本信息: #sqlite3 -version 二.sqlite3常用命令 1.当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端,以sqlite>前缀标识: 2. ...
- SQLite 常用函数
SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...
- sqlmap选项卡介绍及常用语句
sqlmap的使用方式:python sqlmap.py [options]: sqlmap中一共有以下十六个选项卡: 1.帮助选项卡: 2.Target(目标选项卡): 3.Request(请求选项 ...
- SQLite入门语句之HAVING和DISTINCT
一.SQLite入门语句之HAVING HAVING 子句允许指定条件来过滤将出现在最终结果中的分组结果. WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建 ...
- SQL server 常用语句
SQL Server中常用的SQL语句 1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...
随机推荐
- LayaAir引擎——(六)
LayaAir引擎——TiledMap地图图块属性获取和进行墙壁碰撞检测 需要的软件: TiledMap LayaAir IDE 1.0.2版本 所画的地图: pass层: floor层: pass层 ...
- Pyhon环境搭建-window
1.安装python3.4.3版本 地址:(64位)https://www.python.org/ftp/python/3.4.3/python-3.4.3.amd64.msi (32位)http:/ ...
- sass的安装与使用
一.SASS简介:SASS是一种使CSS的开发,变得简单和可维护开发工具. 二.安装和使用 2.1 SASS的安装: 安装sass之前首先需要安装ruby,ruby的安装可以直接在百度搜索安装,安装 ...
- Python——Day3知识点——文件操作
一.打开文件 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ...
- awk(1)-简述
1.概述 AWK is a programming language designed for text processing and typically used as a data extract ...
- QT显示歌词渐变
central = new QWidget(this); setCentralWidget(central); central->setAutoFillBackground(true); cen ...
- Unattend.xml应答文件制作(WISM)
将制作好的应答文件unattend.xml拷贝到模板机sysprep目录下,然后在cmd下运行 (unattend.xml文件可自定义名称) sysprep /generalize /oobe / ...
- 使用 FP-growth 算法高效挖掘海量数据中的频繁项集
前言 对于如何发现一个数据集中的频繁项集,前文讲解的经典 Apriori 算法能够做到. 然而,对于每个潜在的频繁项,它都要检索一遍数据集,这是比较低效的.在实际的大数据应用中,这么做就更不好了. 本 ...
- Mac 配置Spark环境scala+python版本(Spark1.6.0)
1. 从官网下载Spark安装包,解压到自己的安装目录下(默认已经安装好JDK,JDK安装可自行查找): spark官网:http://spark.apache.org/downloads.html ...
- [c++] vector的使用
} { vec.push_back(value); } { vector< vector<,); vector<, sec ...