当前位置:Gxlcms > 数据库问题 > sqlite3

sqlite3

时间:2021-07-01 10:21:17 帮助过:5人阅读

//  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

标签:imp   height   form   ict   ping   direct   sar   exe   ble   

人气教程排行