SQLite3数据库

sqlite3采用sqlite数据库存储数据
支持sql语句,方便查询
 
插件:Mesasqlite
 
//
//  ViewController.m
//  sqliteTest
//
//  Created by jerehedu on 15/2/2.
//  copyright (c) 2015年 jereh. All rights reserved.
//

#import "ViewController.h"
//1.导入sqlite3文件
#import <sqlite3.h>
@interface ViewController ()
{
    //2.声明sqlite3对象
    sqlite3 *db;
}
@end

#pragma mark 获得沙盒路径
- (Nsstring *)getUserDocumentPath
{
    //获得沙盒路径
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    Nsstring *documentPath = [path lastObject];
    return documentPath;
}

#pragma mark 追加路径
- (Nsstring *)appendingPathComponent:(Nsstring *)documentPath andFileName:(Nsstring *)fileName
{
    Nsstring *sqlitePath = [documentPath stringByAppendingPathComponent:fileName];
    return sqlitePath;
}

#pragma mark 创建或打开数据库
-(BOOL)openorCreatesqliteWithDBPath:(Nsstring *)dbpath{
    const char *p = [dbpath UTF8String];
    int res = sqlite3_open(p,&db);
    if (res == sqlITE_OK) {
        return YES;
    }else{
        return NO;
    }
}

#pragma mark 执行sql语句
-(BOOL)execsqlNoQueryWithsql:(Nsstring *)sql{
    int res = sqlite3_exec(db,[sql UTF8String],NULL,NULL);
    if (res == sqlITE_OK){
        return YES;
    }else{
        return NO;
    }
}

#pragma mark 返回stmt,无参数
-(sqlite3_stmt *)execQueryWithsql:(Nsstring *)sql{
    sqlite3_stmt *stmt;
    int pre_res = sqlite3_prepare_v2(db,-1,&stmt,NULL);
    if (pre_res == sqlITE_OK){
        return stmt;
    }
    return NULL;
}

#pragma mark 有参数
-(sqlite3_stmt *)execQueryWithsql:(Nsstring *)sql andWithParams:(NSArray *)params{
    sqlite3_stmt *stmt;
    int pre_res = sqlite3_prepare_v2(db,NULL);
    if (pre_res == sqlITE_OK){
        //绑定参数
        //判断参数列表是否为空
        if (params != nil) {
            //循环绑定参数
            for (int i = 0; i < params.count; i++) {
                //                sqlite3_bind_int(stmt,i+1,[params[i] intValue]);
                //要判断参数类型
                id obj = params[i];
                if (obj == nil) {
                    sqlite3_bind_null(stmt,i+1);
                }
                else if ([obj respondsToSelector:@selector(objCType)]){
                    //respondsToSelector判断对象是否包含objCType方法(数字有)
                    //strstr(char *,char *)判断是否在char中出现过
                    if (strstr("ilsILS",[obj objCType])) {
                        sqlite3_bind_int(stmt,[obj intValue]);
                    }else if (strstr("fd",[obj objCType])) {
                        sqlite3_bind_double(stmt,[obj doubleValue]);
                    }else{
                        stmt = NULL;
                    }
                }else if ([obj respondsToSelector:@selector(UTF8String)]){
                    sqlite3_bind_text(stmt,[obj UTF8String],NULL);
                }else{
                    stmt = NULL;
                }
            }
        }
        
        return stmt;
    }
    return NULL;
}

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
//获得沙盒路径
    Nsstring *dbPath;
    dbPath = [self getUserDocumentPath];
    
    Nsstring *sqlitePath;
    sqlitePath = [self appendingPathComponent:dbPath andFileName:@"test.sqlite"];//追加路径(实际不存在)
    NSLog(@"%@",dbPath);
//5.判断是否打开成功
    if ([self openorCreatesqliteWithDBPath:sqlitePath]) {
        NSLog(@"db is open");
        //构造sql语句
        Nsstring *sql = @"create table if not exists demo(d_id integer primary key autoincrement,d_name varchar(20))";
        int exec_res = sqlite3_exec(db,NULL);
        if (exec_res == sqlITE_OK) {
            NSLog(@"table is created");
        }
        /*
         参数:
         1.sqlite3 对象
         2.sql语句
         3.回调函数
         4.回调函数的参数
         5.错误信息
         */
        
        //插入记录,构造sql语句
        Nsstring *insert_sql = @"insert into demo(d_name) values ('zhangziyao')";
if ([self execsqlNoQueryWithsql:insert_sql]) {
            NSLog(@"one recoder is inserted");
        }
        
        //修改
        Nsstring *update_sql = @"update demo set d_name = 'chenyi' where d_id = 1";
if ([self execsqlNoQueryWithsql:update_sql]) {
            NSLog(@"one recoder is updated");
        }
        
        //删除
        Nsstring *delete_sql = @"delete from demo where d_name='wuyuqiu'";
if ([self execsqlNoQueryWithsql:delete_sql]) {
            NSLog(@"one recoder is deleted");
        }
        
        //查询
int search_d_id = 3;
        Nsstring *search_name = @"_h%";
        Nsstring *search_sql = @"select * from demo where d_id>? and d_name like ?";
sqlite3_stmt *stmt;
if ([self execQueryWithsql:search_sql]) {
            stmt = [self execQueryWithsql:search_sql andWithParams:@[[NSNumber numberWithInt: search_d_id],search_name]];
            while (sqlite3_step(stmt) == sqlITE_ROW) {
                int d_id = sqlite3_column_int(stmt,0);
                const unsigned char *d_name = sqlite3_column_text(stmt,1);
                Nsstring *name = [Nsstring stringWithUTF8String:(char*)d_name];
                NSLog(@"id=%d,name=%@",d_id,name);
                _idLabel.text = [Nsstring stringWithFormat:@"id=%d",d_id];
                _nameLabel.text = [Nsstring stringWithFormat:@"name=%@",name];
 }
        }
        //释放stmt
        sqlite3_finalize(stmt);
        
        //关闭数据库
        sqlite3_close(db);
        
        NSNumber *n = [NSNumber numberWithDouble:1.0];
        NSLog(@"%s",[n objCType]);
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // dispose of any resources that can be recreated.
}

@end

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能