IOS developmentLearning database documentation for IOSDatabaseEncapsulation is the content to be introduced in this article.IOS developmentFor more information about database encapsulation, see this article. There is not much content, mainly implemented by code.
H file
- # 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 file
-
- # 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];
- }
- // Create a database
-
- -(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 (@ "database creation failed ");
- Return NO;
- }
- Return YES;
- }
-
- // Create a table
-
- -(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 (@ "failed to create data table: % s", errorMsg );
- Return NO;
- }
- [SelfcloseDatabase];
- Return YES;
- }
- // Close the database
- -(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 ("failed to update table: % 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] initwithuf8string: dbResult [index];
- NSString * key = [[NSStringalloc] initwithuf8string: 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] initwithuf8string: columnName [I];
- NSString * value = [[NSStringalloc] initwithuf8string: 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 ("query error: % s", errorMsg );
- }
- [SelfcloseDatabase];
- Return [arrayResult autorelease];
- }
- @ End
Summary: AnalysisIOS developmentLearningDatabaseDocument for IOSDatabaseThe encapsulated content has been introduced. I hope this article will help you!