@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的更多相关文章

随机推荐

  1. CF #EDU R1 E

    最二的一次了~我开始以为是带有贪心的DP,谁知道想错了.后来才想明白,暴力二分+记忆化DP #include <iostream> #include <cstdio> #inc ...

  2. MySQL 调优 —— Using filesort

    出现这个问题的解决办法在于 MySQL 每次查询仅仅能使用一个索引, 而你的 SQL 语句 WHERE 条件和 ORDER BY 的条件不一样, 索引没建好的话. 那么 ORDER BY 就使用不到索 ...

  3. web压力測试-Web Bench

    1.web bench下载.地址:http://home.tiscali.cz/~cz210552/webbench.html 2.wen bench安装: [root@web111 tmp]#tar ...

  4. 2015 年度新增开源软件排名TOP100

    本榜单包括 2015 年开源中国新收录的 5977 款开源软件中,依据软件本身的关注度.活跃程度进行排名前 100 名的软件.从这份榜单中也许能够了解到最新业界的趋势. 1.SwitchyOmega ...

  5. iOS-UIWebview比例缩放

    你在使用UIWebview显示网页时.可能会注意到.UIWebView所支持的缩放倍率是非常有限的.而在Safari自己所支持的缩放系数比UIWebview要大得多. 本文解释了怎样加大UIWebVi ...

  6. android在学习——activity关闭和dialog.dismiss冲突的解决(Activity has leaked window com.android.internal.policy.impl.PhoneWindow)

    当我们在退出整个程序的时候偶尔会出现这种报错:Activity has leaked window com.android.internal.policy.impl.PhoneWindow 其意思大概 ...

  7. [RK3288][Android6.0] 调试笔记 --- 系统第一次开机进入Recovery模式原因【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/53464461 latform: ROCKCHIPOS: Android 6.0Kernel: ...

  8. selenium3 + python 操作浏览器基本方法

    from selenium import webdriverimport time as t # driver = webdriver.Chrome()# driver.get("http: ...

  9. LocalDateTime相关处理,得到零点以及24点值,最近五分钟点位,与Date互转,时间格式

    最近一直使用LocalDateTime,老是忘记怎么转换,仅此记录一下 import java.time.Instant; import java.time.LocalDateTime; import ...

  10. 数据通讯与网络 第五版第24章 传输层协议-UDP协议部分要点

    24.1 介绍 本章节主要集中于传输层协议的解读,图24.1展示TCP.UDP.SCTP在TCP\IP协议栈的位置 24.1.1 服务(Service) 每个协议都提供不同的服务,所以应该合理正确的使 ...