IOS 下SQLite3数据库的连接工具类

本文介绍了一个用于iOS应用中操作SQLite3数据库的Objective-C类。该类提供了一种简单的方法来执行SQL查询并获取结果集。通过示例代码展示了如何使用这个类来连接数据库、执行查询并处理结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


声明:本代码是在xcode4.2下编写,如要在他之下版本下编译,需要修改一小部分代码及加上手动内存管理

    代码的主要用途是在IOS下面读取sqlite3的数据库。


本代码Demo以放到我个人的优快云资源库里,可前往下载:http://download.youkuaiyun.com/detail/ch_soft/4020052 点击打开链接

默认的,还有一个资源分,本想免费的,但好像不能修改,只能重传,我就省的麻烦了。


一、使用方法

  sqliteHelper*sh=[[sqliteHelperalloc]init];

   sh.databasePath=path;

    if ([sh open]) {

        

        NSMutableArray *arr=[sh excelSql:query andParamCount:7];

        [shclose];

    }


一,源文件


//

//  sqliteHelper.h

//  TestSqlite

//

//  Created by ch_soft on 12-1-13.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import<UIKit/UIKit.h>

#import"sqlite3.h"

@interface sqliteHelper :NSObject 

{    

}         

@property (strong,nonatomic)NSString *databaseName;

@property (strong,nonatomic)NSString *databasePath;

@property (nonatomic)sqlite3_stmt *statement;

-(BOOL)open;

-(void)close;


//query:是查新的SQl

//cout:是参数个数

// 如:query 为 select a,b,c from db;参数个是为3个(a,b,c)

-(NSMutableArray *) excelSql:(NSString *) query andParamCount:(NSInteger) cout;

@end




//

//  sqliteHelper.m

//  TestSqlite

//

//  Created by ch_soft on 12-1-13.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "sqliteHelper.h"

#import "sqlite3.h"

@implementation sqliteHelper


@synthesize databaseName;

@synthesize databasePath;

@synthesize statement;

static sqlite3 *database_ =nil;//定义个全局的变量

-(BOOL)open

{

    NSFileManager *fileManager = [NSFileManagerdefaultManager];

    BOOL find = [fileManager fileExistsAtPath:databasePath];//首先判断sqlite数据库文件是否存在

    if (find) {

        if (sqlite3_open([databasePathUTF8String], &database_) !=SQLITE_OK) {//如何打开数据库失败

            sqlite3_close(database_);

            NSLog(@"Error: open database error.");

            return NO;

        }

        return YES;

    }else

    {

        sqlite3_close(database_);

        NSLog(@"Error: open file error.");

        return NO;

    }

    return NO;

}

-(void)close

{

     sqlite3_finalize(statement);

     sqlite3_close(database_);

}


-(NSString*)getColumn:(NSInteger) index

{

    char *temp =(char*)sqlite3_column_text(statement, index);

    NSString * retValue=[NSStringstringWithUTF8String:temp];

    return retValue;

}


-(NSMutableArray *) excelSql:(NSString *) query andParamCount:(NSInteger) cout

{

    NSMutableArray * retArr=[NSMutableArrayarray];

    if(sqlite3_prepare_v2(database_, [queryUTF8String], -1, &statement,nil) == SQLITE_OK)

{

while(sqlite3_step(statement) == SQLITE_ROW)

{

            NSMutableArray * ary=[NSMutableArrayarray];

            for (int i=1; i<=cout; i++) {

                NSString * oneValue=[selfgetColumn:i];

                NSLog(@"one value is %@",oneValue);

                [ary addObject:oneValue];

            }

[retArr addObject:ary];

}

}

    return retArr;

}


@end



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值