微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlite1-2增 删 改 查方法的简单封装

//  sqliteHelper.m
//  sqliteText3
//
//  Created by jerehedu on 15/2/3.
//  copyright (c) 2015年 baidu. All rights reserved.
//

#import "sqliteHelper.h"
#import <sqlite3.h>


#define  dbnAME @"demo.sqlite"

@implementation sqliteHelper
{
    sqlite3 *db;//2. 声明sqlite3对象
}

- (Nsstring *)getUserDocumentPath
{
    //3. 转换沙盒路径为C字符串
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    Nsstring *documentPath=[path lastObject];
    return documentPath;
}

- (Nsstring *)appendingPathComponent:(Nsstring *)documentPath andFileName:(Nsstring*)fileName
{
    Nsstring *dbPath = [documentPath stringByAppendingPathComponent:fileName];
    return dbPath;
}
- (BOOL)openorCreatesqliteDBWithDBPathWithFileName:(Nsstring*)fileName
{
    Nsstring *documentPath = [self getUserDocumentPath];
    Nsstring *sqlitePath = [self appendingPathComponent:documentPath andFileName:fileName];

    const char *p = [sqlitePath UTF8String];
    
    //4. 打开或创建数据库串
    int res = sqlite3_open(p,&db);
    if (res == sqlITE_OK) {
        return YES;
    }
    return NO;
}
#pragma mark 增 删 改
- (BOOL)execsqlNoQueryWith:(Nsstring*)sql
{
    [self openorCreatesqliteDBWithDBPathWithFileName:dbnAME];
    BOOL isExecSuccess = NO;
    int insert_res = sqlite3_exec(db,[sql UTF8String],NULL,NULL);
    if (insert_res == sqlITE_OK) {
        isExecSuccess =YES;
    }
    sqlite3_close(db);
    return isExecSuccess;
}

- (BOOL)execsqlNoQueryWith:(Nsstring*)sql andWithParams:(NSArray*)params
{
    BOOL isExecSuccess = NO;
    sqlite3_stmt *stmt = [self prepareStatementWithsql:sql andWithParams:params];
    if (sqlite3_step(stmt)==sqlITE_DONE) {
        isExecSuccess = YES;
    }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return isExecSuccess;
}

// <2>.......
- (sqlite3_stmt*)prepareStatementWithsql:(Nsstring*)sql andWithParams:(NSArray*)params
{
    [self openorCreatesqliteDBWithDBPathWithFileName:dbnAME];
    sqlite3_stmt *stmt;
    int pre_res = sqlite3_prepare_v2(db,-1,&stmt,NULL);
    if (pre_res == sqlITE_OK) {
        //绑定参数
        //先判断是否参数为nil,然后循环绑定多个参数
        if (params != nil)
        {
            for (int i = 0; i <[params count]; i++)
            {
                id obj = params[i];
                if (obj == nil) {
                    sqlite3_bind_null(stmt,i+1);
                }
                else if ([obj respondsToSelector:@selector(objCType)]){
                    //respondsToSelector判断是否有objCType方法,返回BOLL型
                    if (strstr("ilsILS",[obj objCType] )){
                        //strstr (char *,char*)判断是否在char*中出现过
                        sqlite3_bind_int(stmt,i+1,[obj intValue]);
                    }
                    else if (strstr("fdFD",[obj objCType])){
                        sqlite3_bind_double(stmt,[obj doubleValue]);
                    }
                    else{
                        stmt = nil;
                    }
                }
                else if([obj respondsToSelector:@selector(UTF8String)]){
                    sqlite3_bind_text(stmt,[obj UTF8String],NULL);
                    
                }else{
                    stmt = nil;
                }
            }
        }
        return stmt;
    }
    return NULL;
}

- (void)closeDB{
    if (db) {
        sqlite3_close(db);
    }
    
}

@end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


SQLite架构简单,又有Json计算能力,有时会承担Json文件/RESTful的计算功能,但SQLite不能直接解析Json文件/RESTful,需要用Java代码硬写,或借助第三方类库,最后再拼成insert语句插入数据表,代码非常繁琐,这里就不展示了。参考前面的代码可知,入库的过程比较麻烦,不能只用SQL,还要借助Java或命令行。SPL是现代的数据计算语言,属于简化的面向对象的语言风格,有对象的概念,可以用点号访问属性并进行多步骤计算,但没有继承重载这些内容,不算彻底的面向对象语言。...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。本教程将告诉您如何使用 SQLite 编程,并让你迅速上手。.................................
安卓开发,利用SQLite实现登陆注册功能
相比大多数数据库而言,具有等优势,广泛应用于、等领域。
有时候,一个项目只有一个数据库,比如只有SQLite,或者MySQL数据库,那么我们只需要使用一个固定的数据库即可。但是一个项目如果写好了,有多个用户使用,但是多个用户使用不同的数据库,这个时候,我们就需要把软件设计成可以连接多个数据库的模式,用什么数据库,就配置什么数据库即可。4.Users实体类,这个实体类要和数据库一样的,形成一一对应的关系。11.Sqlite数据库,需要在代码里面创建数据库,建立表,再建立数据。8.我们开启MySQL数据库,然后进行调试,看程序的结果。2.安装SqlSugar。
基于Android的背单词软件,功能强大完整。
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统。说白了就是使用起来轻便简单,