IOS开发学习数据库文档之IOS数据库封装是本文要介绍的内容,主要来学习IOS开发中数据库的封装,具体内容来看本文详解。内容不多,主要是以代码来实现。
H文件
- #import <Foundation/Foundation.h>
- #import "sqlite3.h"
- @interface DatabaseOperation : NSObject {
- sqlite3 *m_sql;
- NSString *m_dbName;
- }
- @property(nonatomic)sqlite3* m_sql;
- @property(nonatomic,retain)NSString* m_dbName;
- -(id)initWithDbName:(NSString*)dbname;
- -(BOOL)openOrCreateDatabase:(NSString*)DbName;
- -(BOOL)createTable:(NSString*)sqlCreateTable;
- -(void)closeDatabase;
- -(BOOL)InsertTable:(NSString*)sqlInsert;
- -(BOOL)UpdataTable:(NSString*)sqlUpdata;
- -(NSArray*)querryTable:(NSString*)sqlQuerry;
- -(NSArray*)querryTableByCallBack:(NSString*)sqlQuerry;
- @end
- M文件
- #import "DatabaseOperation.h"
- @implementation DatabaseOperation
- @synthesize m_sql;
- @synthesize m_dbName;
- - (id) initWithDbName:(NSString*)dbname
- {
- self = [super init];
- if (self != nil) {
- if ([self openOrCreateDatabase:dbname]) {
- [self closeDatabase];
- }
- }
- return self;
- }
- - (id) init
- {
- NSAssert(0,@"Never Use this.Please Call Use initWithDbName:(NSString*)");
- return nil;
- }
- - (void) dealloc
- {
- self.m_sql = nil;
- self.m_dbName =nil;
- [super dealloc];
- }
- //创建数据库
- -(BOOL)openOrCreateDatabase:(NSString*)dbName
- {
- self.m_dbName = dbName;
- NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
- NSString *documentsDirectory = [path objectAtIndex:0];
- if(sqlite3_open([[documentsDirectorystringByAppendingPathComponent:dbName] UTF8String],&m_sql) !=SQLITE_OK)
- {
- NSLog(@"创建数据库失败");
- return NO;
- }
- return YES;
- }
- //创建表
- -(BOOL)createTable:(NSString*)sqlCreateTable
- {
- if (![selfopenOrCreateDatabase:self.m_dbName]) {
- return NO;
- }
- char *errorMsg;
- if (sqlite3_exec (self.m_sql, [sqlCreateTable UTF8String],NULL, NULL, &errorMsg) != SQLITE_OK)
- {
- NSLog(@"创建数据表失败:%s",errorMsg);
- return NO;
- }
- [selfcloseDatabase];
- return YES;
- }
- //关闭数据库
- -(void)closeDatabase
- {
- sqlite3_close(self.m_sql);
- }
- //insert
- -(BOOL)InsertTable:(NSString*)sqlInsert
- {
- if (![selfopenOrCreateDatabase:self.m_dbName]) {
- return NO;
- }
- char* errorMsg = NULL;
- if(sqlite3_exec(self.m_sql, [sqlInsertUTF8String],0, NULL, &errorMsg) ==SQLITE_OK)
- { [selfcloseDatabase];
- returnYES;}
- else {
- printf("更新表失败:%s",errorMsg);
- [selfcloseDatabase];
- return NO;
- }
- return YES;
- }
- //updata
- -(BOOL)UpdataTable:(NSString*)sqlUpdata{
- if (![selfopenOrCreateDatabase:self.m_dbName]) {
- return NO;
- }
- char *errorMsg;
- if (sqlite3_exec (self.m_sql, [sqlUpdata UTF8String],0, NULL, &errorMsg) !=SQLITE_OK)
- {
- [selfcloseDatabase];
- returnYES;
- }else {
- returnNO;
- }
- return YES;
- }
- //select
- -(NSArray*)querryTable:(NSString*)sqlQuerry
- {
- if (![selfopenOrCreateDatabase:self.m_dbName]) {
- return nil;
- }
- int row = 0;
- int column = 0;
- char* errorMsg = NULL;
- char** dbResult = NULL;
- NSMutableArray* array = [[NSMutableArrayalloc] init];
- if(sqlite3_get_table(m_sql, [sqlQuerryUTF8String], &dbResult, &row,&column,&errorMsg ) == SQLITE_OK)
- {
- if (0 == row) {
- [self closeDatabase];
- return nil;
- }
- int index = column;
- for(int i =0; i < row ; i++ ) {
- NSMutableDictionary* dic = [[NSMutableDictionaryalloc] init];
- for(int j =0 ; j < column; j++ ) {
- if (dbResult[index]) {
- NSString* value = [[NSStringalloc] initWithUTF8String:dbResult[index]];
- NSString* key = [[NSStringalloc] initWithUTF8String:dbResult[j]];
- [dic setObject:value forKey:key];
- [value release];
- [key release];
- }
- index ++;
- }
- [array addObject:dic];
- [dic release];
- }
- }else {
- printf("%s",errorMsg);
- [selfcloseDatabase];
- return nil;
- }
- [selfcloseDatabase];
- return [array autorelease];
- }
- //select
- int processData(void* arrayResult,int columnCount,char** columnValue,char** columnName)
- {
- int i;
- NSMutableDictionary* dic = [[NSMutableDictionaryalloc] init];
- for( i = 0 ; i < columnCount; i ++ )
- {
- if (columnValue[i]) {
- NSString* key = [[NSStringalloc] initWithUTF8String:columnName[i]];
- NSString* value = [[NSStringalloc] initWithUTF8String:columnValue[i]];
- [dic setObject:value forKey:key];
- }
- }
- [(NSMutableArray*)arrayResult addObject:dic];
- [dic release];
- return 0;
- }
- //select
- -(NSArray*)querryTableByCallBack:(NSString*)sqlQuerry
- {
- if (![selfopenOrCreateDatabase:self.m_dbName]) {
- return nil;
- }
- char* errorMsg = NULL;
- NSMutableArray* arrayResult = [[NSMutableArrayalloc] init];
- if (sqlite3_exec(self.m_sql,[sqlQuerryUTF8String],processData,(void*)arrayResult,&errorMsg) !=SQLITE_OK) {
- printf("查询出错:%s",errorMsg);
- }
- [selfcloseDatabase];
- return [arrayResult autorelease];
- }
- @end
小结:解析IOS开发学习数据库文档之IOS数据库封装的内容介绍完了,希望通过本文的学习鞥对你有所帮助!