首先引用libsqlite3.0.dylib库到Frameworks中,然后 加入#import <sqlite3.h>,就可以进行以下操作了!
实现
- #define insertGroup @"INSERT INTO tbl_Group (GroupName) VALUES ('%@')"
- #define deleteGroup @"DELETE from tbl_Group WHERE tbl_Group.GroupID=%d"
- #define updateGroupName @"UPDATE tbl_Group SET GroupName = '%@' WHERE GroupID = %d"
- #define selectGroup @"SELECT tbl_Group.GroupID, GroupName , count(tbl_GroupContact.GroupID) as GroupNumber FROM tbl_Group LEFT JOIN tbl_GroupContact on tbl_Group.GroupID = tbl_GroupContact.GroupID Group By GroupName,tbl_Group.GroupID Order BY tbl_Group.GroupID DESC"
- @implementation SqliteOperation
- - (id)disconnect {
- sqlite3_close(database);
- return nil;
- }
- -(id) init
- {
- if((self = [super init]))
- {
- NSError **error;
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"/MultiMeeting.db"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if(![fileManager fileExistsAtPath:dbPath])
- {
- // Copy the database from the package to the users filesystem
- [fileManager copyItemAtPath:pathDataBasePath toPath:dbPath error:error];
- }
- const char *dbPathChar = [dbPath UTF8String];
- if(sqlite3_open(dbPathChar,&database) == SQLITE_OK)
- {
- return self;
- }
- else
- {
- [self disconnect];
- return nil;
- }
- }
- return nil;
- }
- - (BOOL) NoneQueryWithSqlString:(NSString*) sqlString
- {
- const char *noneQuerySqlChar = [sqlString UTF8String];
- sqlite3_stmt *noneQueryStatement;
- if(sqlite3_prepare_v2(database,noneQuerySqlChar,-1,&noneQueryStatement,NULL) == SQLITE_OK)
- {
- int success = sqlite3_step(noneQueryStatement);
- if (success == SQLITE_DONE) {
- sqlite3_finalize(noneQueryStatement);
- return YES;
- }
- sqlite3_reset(noneQueryStatement);
- }
- sqlite3_finalize(noneQueryStatement);
- return NO;
- }
- //添加组
- -(BOOL) InsertGroup:(EntityGroup*)group
- {
- NSString *insertString = [NSString stringWithFormat:insertGroup,group.GroupName];
- BOOL result = [self NoneQueryWithSqlString:insertString];
- return result;
- }
- -(BOOL) DeleteGroup:(NSInteger) groupID
- {
- NSString *sqlStringForGroup = [NSString stringWithFormat:deleteGroup,groupID];
- return [self NoneQueryWithSqlString:sqlStringForGroup];
- }
- - (BOOL) ModifyGroupName:(EntityGroup*)group
- {
- NSString *sqlString = [NSString stringWithFormat:updateGroupName,group.GroupName,group.GroupID];
- return [self NoneQueryWithSqlString:sqlString];
- }
- //查询群组信息
- -(NSArray*) SelectGroup
- {
- sqlite3_stmt *statement;
- NSMutableArray *GroupArray = nil;
- NSString *sql = selectGroup;
- const char *sql_char = [sql UTF8String];
- if (database != nil) {
- if (sqlite3_prepare_v2(database, sql_char, -1, &statement, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(statement) == SQLITE_ROW) {
- if(GroupArray == nil)
- GroupArray = [[NSMutableArray alloc] init];
- EntityGroup *group = [[EntityGroup alloc] init];
- group.GroupID = sqlite3_column_int(statement, 0);
- group.GroupName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
- group.GroupNmuber = sqlite3_column_int(statement, 2);
- [GroupArray addObject:group];
- [group release];
- }
- if (GroupArray !=nil) {
- sqlite3_finalize(statement);
- [GroupArray autorelease];
- return GroupArray;
- }
- }
- sqlite3_finalize(statement);
- return nil;
- }
- else {
- [self disconnect];
- }
- return nil;
- }
- - (void) dealloc
- {
- [self disconnect];
- [super dealloc];
- }
- @end