我也是在网络上找的,自己加了锁,支持线程安全
.h文件
#import <Foundation/Foundation.h>
#import "sqlite3.h"
@interface DatabaseOperation : NSObject {
sqlite3 *m_sql;
NSString *m_dbName;
NSLock* m_lcok;
}
@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;
-(BOOL)DeldeteTable:(NSString*)sqlDelete;
-(NSArray*)querryTable:(NSString*)sqlQuerry;
-(NSArray*)querryTableByCallBack:(NSString*)sqlQuerry;
@end
.m文件
#import "DatabaseOperation.h"
struct lockhelp
{
lockhelp(NSLock* p):lock_((NSLock*)p) {[lock_ lock];}
~lockhelp() {[lock_ unlock];}
private:
NSLock* lock_;
};
@implementation DatabaseOperation
@synthesize m_sql;
@synthesize m_dbName;
- (id) initWithDbName:(NSString*)dbname
{
self = [super init];
if (self != nil) {
m_lcok = [[NSLock alloc] init];
if ([self openOrCreateDatabase:dbname]) {
[self closeDatabase];
}
}
return self;
}
- (id) init
{
NSAssert(0,@"Never Use this.Please Call Use initWithDbName:(NSString*)");
return nil;
}
- (void) dealloc
{
[m_lcok unlock];
[m_lcok release];
self.m_sql = nil;
self.m_dbName =nil;
[super dealloc];
}
//-------------------创建数据库-------------------------
-(BOOL)openOrCreateDatabase:(NSString*)dbName
{
lockhelp lock(m_lcok);
self.m_dbName = dbName;
NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [path objectAtIndex:0];
if(sqlite3_open([[documentsDirectory stringByAppendingPathComponent:dbName]UTF8String],&m_sql) !=SQLITE_OK)
{
NSLog(@"创建数据库失败");
return NO;
}
return YES;
}
//------------------创建表----------------------
-(BOOL)createTable:(NSString*)sqlCreateTable
{
if (![self openOrCreateDatabase:self.m_dbName]) {
return NO;
}
{
lockhelp lock(m_lcok);
char *errorMsg;
if (sqlite3_exec (self.m_sql, [sqlCreateTable UTF8String],NULL,NULL, &errorMsg) != SQLITE_OK)
{
NSLog(@"创建数据表失败:%s",errorMsg);
return NO;
}
}
[self closeDatabase];
return YES;
}
//----------------------关闭数据库-----------------
-(void)closeDatabase
{
sqlite3_close(self.m_sql);
}
//------------------insert-------------------
-(BOOL)InsertTable:(NSString*)sqlInsert
{
if (![self openOrCreateDatabase:self.m_dbName]) {
return NO;
}
lockhelp lock(m_lcok);
char* errorMsg = NULL;
if(sqlite3_exec(self.m_sql, [sqlInsert UTF8String],0,NULL, &errorMsg) ==SQLITE_OK)
{ [self closeDatabase];
return YES;}
else {
printf("更新表失败:%s",errorMsg);
[self closeDatabase];
return NO;
}
return YES;
}
//--------------updata-------------
-(BOOL)UpdataTable:(NSString*)sqlUpdata{
if (![self openOrCreateDatabase:self.m_dbName]) {
return NO;
}
lockhelp lock(m_lcok);
char *errorMsg;
if (sqlite3_exec (self.m_sql, [sqlUpdata UTF8String],0,NULL, &errorMsg) !=SQLITE_OK)
{
[self closeDatabase];
return YES;
}else {
return NO;
}
return YES;
}
//--------------delete--------------
-(BOOL)DeldeteTable:(NSString*)sqlDelete
{
if (![self openOrCreateDatabase:self.m_dbName]) {
return NO;
}
lockhelp lock(m_lcok);
char *errorMsg;
if (sqlite3_exec (self.m_sql, [sqlDelete UTF8String],0,NULL, &errorMsg) !=SQLITE_OK)
{
[self closeDatabase];
return YES;
}else {
return NO;
}
return YES;
}
//--------------select---------------------
-(NSArray*)querryTable:(NSString*)sqlQuerry
{
if (![self openOrCreateDatabase:self.m_dbName]) {
return nil;
}
lockhelp lock(m_lcok);
int row = 0;
int column = 0;
char* errorMsg = NULL;
char** dbResult = NULL;
NSMutableArray* array = [[NSMutableArray alloc]init];
if(sqlite3_get_table(m_sql, [sqlQuerry UTF8String], &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 = [[NSMutableDictionary alloc]init];
for(int j =0 ; j < column; j++ ) {
if (dbResult[index]) {
NSString* value = [[NSString alloc]initWithUTF8String:dbResult[index]];
NSString* key = [[NSString alloc]initWithUTF8String:dbResult[j]];
[dic setObject:value forKey:key];
[value release];
[key release];
}
index ++;
}
[array addObject:dic];
[dic release];
}
}else {
printf("%s",errorMsg);
[self closeDatabase];
return nil;
}
[self closeDatabase];
return [array autorelease];
}
//----------------------select--------------------
int processData(void* arrayResult,int columnCount,char** columnValue,char** columnName)
{
int i;
NSMutableDictionary* dic = [[NSMutableDictionary alloc]init];
for( i = 0 ; i < columnCount; i ++ )
{
if (columnValue[i]) {
NSString* key = [[NSString alloc]initWithUTF8String:columnName[i]];
NSString* value = [[NSString alloc]initWithUTF8String:columnValue[i]];
[dic setObject:value forKey:key];
}
}
[(NSMutableArray*)arrayResult addObject:dic];
[dic release];
return 0;
}
//---------------------select-----------------------
-(NSArray*)querryTableByCallBack:(NSString*)sqlQuerry
{
if (![self openOrCreateDatabase:self.m_dbName]) {
return nil;
}
lockhelp lock(m_lcok);
char* errorMsg = NULL;
NSMutableArray* arrayResult = [[NSMutableArray alloc]init];
if (sqlite3_exec(self.m_sql,[sqlQuerry UTF8String],processData,(void*)arrayResult,&errorMsg) !=SQLITE_OK) {
printf("查询出错:%s",errorMsg);
}
[self closeDatabase];
return [arrayResult autorelease];
}
@end
有问题的可以留言!