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的更多相关文章
随机推荐
- CODEVS1533 Fibonacci数列 (矩阵乘法)
嗯,,,矩阵乘法最基础的题了. Program CODEVS1250; ..,..] of longint; var T,n,mo:longint; a,b:arr; operator *(a,b:a ...
- linux -- 视频尺寸-cif、2cif、dcif、D1、HD1、4D1
1 CIF简介 CIF是常用的标准化图像格式(Common Intermediate Format).在H.323协议簇中,规定了视频采集设备的标准采集分辨率.CIF = 352×288像素 ...
- 使用JQUERY的flexselect插件来实现将SELECT下拉菜单变成自动补全输入框
这也是下拉列表太长了之后,使用的同事提出来的意见, 然后,本来开始想将DJANGO的那个后台下拉菜单移植过来的,但发现不现实,也麻烦, 就找了几个JQUERY的插件测试了一下,最后选中了flexsel ...
- F - Goldbach`s Conjecture kuangbin 基础数论
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathemat ...
- [bzoj3061][Usaco13Feb]Partitioning the Farm_动态规划_状压dp
Partitioning the Farm bzoj-3061 Usaco13Feb 题目大意:给定一个n*n的方格图,用k条贯穿方格图的直线将整个方格图分割,使得每一块的权值和的最大值最小. 注释: ...
- HTML5:防止页面在移动设备上缩放
在制作网页时,如果对移动设备有做兼容设计的话,通常是不希望页面在移动设备能够被缩放.这样可以防止原先设计好的样式被破坏.要做到这一点,只需要在网页的head部分加入如下语句即可: <!-- 屏蔽 ...
- PHP array_diff_assoc()
定义和用法 array_diff_assoc() 函数返回两个数组的差集数组.返回的数组的元素都取自被比较的数组(既第一个数组). 和 array_diff() 函数 不同,本函数要求键名和键值都进行 ...
- HDU 5467
第一次写LCT,各种模板加入...以后都只遇到有新意的题目再更新了 这道题就是LCT,但是,难在一个回退的操作.这时,可以通过改变执行顺序,先把要回退后再做的操作先执行了,再回退到之前的执行.这时,建 ...
- LINQ体验(2)——C# 3.0新语言特性和改进(上篇)
整体来说.Visual Studio 2008和.NET 3.5是建立在.NET2.0核心的基础之上,.NET2.0核心本身将不再变化(假设不了解.NET2.0的朋友,请參看MSDN或者一些经典的书籍 ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...