首页 > 代码库 > sqlite3

sqlite3

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #d12f1b } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #78492a } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ba2da2 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { font: 14.0px "PingFang SC" } span.s3 { color: #78492a } span.s4 { color: #d12f1b } span.s5 { color: #ba2da2 } span.s6 { color: #000000 } span.s7 { color: #703daa } span.s8 { color: #3e1e81 } span.s9 { color: #4f8187 } span.s10 { color: #31595d } span.s11 { color: #272ad8 } span.s12 { font: 14.0px "PingFang SC"; color: #d12f1b }</style>

//

//  HRTZhongDuanBiao.m

//  merchant

//

//  Created by Deng Chaojie on 2017/6/8.

//  Copyright ? 2017 Hrtpayment. All rights reserved.

//

 

#import "HRTZhongDuanBiao.h"

#import <sqlite3.h>

 

#define KDBName @"ZhongDuan.sqlite"

#define KbiaoMing @"ZhongDuanBiao"

#define KzhongDuanHao @"zhongDuanHao"

#define KliuShuiHao @"liuShuiHao"

#define KpiCiHao @"piCiHao"

#define KchaXunHao @"chaXunHao"

 

 

@interface HRTZhongDuanBiao ()

 

@property (nonatomic) sqlite3 *sqlite;

@end

 

@implementation HRTZhongDuanBiao

 

static HRTZhongDuanBiao *biao = nil;

+ (HRTZhongDuanBiao *)share

{

    static dispatch_once_t t;

    

    dispatch_once(&t, ^{

        

        biao = [[HRTZhongDuanBiao alloc] init];

        if ([biao openDB]) {

            [biao chuangJianBiao];

        }

        

    });

    return biao;

}

 

- (BOOL)xieRuShuJu:(NSDictionary *)shuJu

{

    NSString *xieRuSql = [NSString stringWithFormat:@"INSERT INTO ‘%@‘ (‘%@‘,‘%@‘, ‘%@‘,‘%@‘) VALUES (‘%@‘,‘%@‘,‘%@‘,‘%@‘)",

                          KbiaoMing,KzhongDuanHao, KliuShuiHao, KpiCiHao, KchaXunHao,shuJu[@"zhongDuanHao"],shuJu[@"liuShuiHao"],shuJu[@"piCiHao"],shuJu[@"chaXunHao"]];

    return [self zhiXingSql:xieRuSql];

}

- (BOOL)gengXinShuJu:(NSDictionary *)shuJu

{

    NSString *gengXinSql = [NSString stringWithFormat:@"update %@ set %@ = ‘%@‘,%@ = ‘%@‘,%@ = ‘%@‘ where %@ = ‘%@‘",KbiaoMing,KliuShuiHao,shuJu[@"liuShuiHao"],KpiCiHao,shuJu[@"piCiHao"],KchaXunHao,shuJu[@"chaXunHao"],KzhongDuanHao,shuJu[@"zhongDuanHao"]];

    return [self zhiXingSql:gengXinSql];

}

- (NSArray *)duQuShuJuWithTid:(NSString *)tid

{

    NSMutableArray *mutableA = [[NSMutableArray alloc] init];

    if ([self openDB]) {

        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ where %@ = ‘%@‘",KbiaoMing,KzhongDuanHao,tid] ;

        if (tid.length == 0) {

            sql = [NSString stringWithFormat:@"SELECT * FROM %@",KbiaoMing] ;

        }

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_sqlite, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)

        {

            

            while (sqlite3_step(statement) == SQLITE_ROW) {

                NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

                NSString *dataid = [[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(statement, 0)];

                [dic setObject:dataid forKey:@"did"];

                

                NSString *zhongDuanHao = [[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(statement, 1)];

                [dic setObject:zhongDuanHao forKey:@"zhongDuanHao"];

                

                NSString *liuShuiHao = [[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(statement, 2)];

                [dic setObject:liuShuiHao forKey:@"liuShuiHao"];

 

                NSString *piCiHao = [[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(statement, 3)];

                [dic setObject:piCiHao forKey:@"piCiHao"];

                

                NSString *chaXunHao = [[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(statement, 4)];

                [dic setObject:chaXunHao forKey:@"chaXunHao"];

                [mutableA addObject:dic];

            }

        }

        sqlite3_finalize(statement);

        sqlite3_close(_sqlite);

    }

    return mutableA;

}

 

- (BOOL)openDB

{

    NSArray *pathA = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *pathDocument = pathA[0];

    NSString *pathDB = [pathDocument stringByAppendingPathComponent:KDBName];

    int jieGuo = sqlite3_open([pathDB UTF8String], &_sqlite);

    if (jieGuo == SQLITE_OK) {

        return YES;

    }else

    {

        sqlite3_close(_sqlite);

    }

    HRTLog(@"openDB 失败");

    return NO;

}

- (void)chuangJianBiao

{

    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (ID INTEGER PRIMARY KEY AUTOINCREMENT,%@ TEXT,%@ TEXT,%@ TEXT,%@ TEXT)",KbiaoMing,KzhongDuanHao,KliuShuiHao,KpiCiHao,KchaXunHao];

    if ([self zhiXingSql:sql]) {

        

    }else

    {

        HRTLog(@"创建表失败");

    }

}

- (BOOL)zhiXingSql:(NSString *)sql

{

    if ([self openDB]) {

        char *err;

        if (sqlite3_exec(_sqlite, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {

            

            HRTLog(@"sqlite3_exec 失败");

            return NO;

        }else{

            return YES;

        }

    }

    sqlite3_close(_sqlite);

    HRTLog(@"zhiXingSql 失败");

    return NO;

}

@end

sqlite3