sqlitManager
@interface sqlitManager : NSObject
+(instancetype)sharedSqlitManager;
-(void)createDB;
-(void)createTabel;
-(void)insertTable;
-(void)checkTable;
@end
#import "sqlitManager.h"
#import <sqlite3.h>
@interface sqlitManager()
{
// 创建数据库句柄
sqlite3 *db;
char *error;
}
@end
@implementation sqlitManager
+(instancetype)sharedSqlitManager
{
static sqlitManager *manager = nil;
@synchronized (self) {
if(manager == nil)
{
manager = [self new];
}
return manager;
}
}
-(void)createDB
{
// 设置数据库文件路径
NSString *databaseFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/mydb.sqlite"];
// // 创建数据库句柄
// sqlite3 *db;
// char *error;
if (sqlite3_open([databaseFilePath UTF8String], &db) == SQLITE_OK) {
NSLog(@"sqlite dadabase is opened.");
} else {
NSLog(@"sqlite dadabase open fail.");
}
}
-(void)createTabel
{
/*
sql 语句,专门用来操作数据库的语句。
create table if not exists 是固定的,如果表不存在就创建。
myTable() 表示一个表,myTable 是表名,小括号里是字段信息。
字段之间用逗号隔开,每一个字段的第一个单词是字段名,第二个单词是数据类型,primary key 代表主键,autoincrement 是自增。
*/
NSString *createSql = @"create table if not exists myTable(id integer primary key autoincrement, name text, age integer, address text)";
if (sqlite3_exec(db, [createSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
NSLog(@"create table is ok.");
} else {
NSLog(@"error: %s", error);
// 每次使用完毕清空 error 字符串,提供给下一次使用
sqlite3_free(error);
}
}
-(void)insertTable;
{
NSString *insertSql = @"insert into myTable(name, age, address) values('小新', '8', '东城区')";
if (sqlite3_exec(db, [insertSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
NSLog(@"insert operation is ok.");
} else {
NSLog(@"error: %s", error);
// 每次使用完毕清空 error 字符串,提供给下一次使用
sqlite3_free(error);
}
NSString *updateSql = @"update myTable set name = '小白', age = '10', address = '西城区' where id = 2";
if (sqlite3_exec(db, [updateSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
NSLog(@"update operation is ok.");
} else {
NSLog(@"error: %s", error);
// 每次使用完毕清空 error 字符串,提供给下一次使用
sqlite3_free(error);
}
}
-(void)checkTable
{
sqlite3_stmt *statement;
// @"select * from myTable" 查询所有 key 值内容
NSString *selectSql = @"select id, name, age, address from myTable";
if (sqlite3_prepare_v2(db, [selectSql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while(sqlite3_step(statement) == SQLITE_ROW) {
// 查询 id 的值
int _id = sqlite3_column_int(statement, 0);
// 查询 name 的值
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
// 查询 age
int age = sqlite3_column_int(statement, 2);
// 查询 name 的值
NSString *address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
NSLog(@"id: %i, name: %@, age: %i, address: %@", _id, name, age, address);
}
} else {
NSLog(@"select operation is fail.");
}
sqlite3_finalize(statement);
}
@end
sqlitManager的更多相关文章
随机推荐
- bupt summer training for 16 #1 ——简单题目
D.What a Mess 给n个数,求其中能满足 a[i] % a[j] == 0 的数对之和 n = 1W,max_ai = 100W 不是很大,所以就直接筛就可以了 计算可得最高复杂度 < ...
- 详解Cookie、LocalStorage、SessionStorage
不管是笔试还是面试相信大家都会经常遇到问Cookie.LocalStorage.SessionStorage 这三个不同的,什么不说先上一波图先: 针对他们大小之分应用场景也有不同: 因为考虑到每个 ...
- uva A Spy in the Metro(洛谷 P2583 地铁间谍)
A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangero ...
- 4.非关系型数据库(Nosql)之mongodb:普通索引,唯一索引
一:普通索引 1创建一个新的数据库 > use toto; switched to db toto > show dbs; admin (empty) local 0.078GB & ...
- TDD尝试:nodejs单元测试
单元测试是最小化的测试方式,也是TDD的做法. TDD概念如下图: 通过测试反馈推进开发,ruby是推崇这种编程方式的. nodejs有如下常用单元测试模块 1.mocha Mocha是一个基于nod ...
- vbs setkeys 特殊符号
set keys={~}!^@^#^${%%}{^&^}{^^}{*}{(}{)}{_}{-}{=}{+}.;:'"
- HDU 5289 Assignment (ST算法区间最值+二分)
题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...
- storm 并行度
1个worker进程运行的是1个topology的子集(注:不会出现1个worker为多个topology服务).1个worker进程会启动1个或多个executor线程来运行1个topology的c ...
- H264的RTP负载打包的数据包格式,分组,分片
H264的RTP负载打包的数据包格式,分组,分片 1. RTP数据包格式 RTP报文头格式(见RFC3550 Page12): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ...
- PCB 线路板人生
由此开端,增加PCB人生分类栏,后续在此分享PCB 非工作方面所思所想,由于文笔不好,请指正.