Fmdb iPhone Database

Source: Internet
Author: User
Fmdb provides the method of accessing sqlite3 using objective-C encapsulation, avoiding the inclusion of C-style code in objective-C Programming (not to say that it cannot be used, but is mixed in different code styles ).
Https://github.com/ccgus/fmdb #import  "FMDatabase.h" #import "FMDatabaseAdditions.h"  @class FMDatabase;  @interface SqliteInterface : NSObject {      NSString *dbRealPath;      FMDatabase *dbo; }  @property (nonatomic, retain) NSString *dbRealPath; @property (nonatomic, retain) FMDatabase *dbo;  + (SqliteInterface *)sharedSqliteInterface; - (void) connectDB; - (void) closeDB; - (void) setupDB : (NSString *)dbFileName;  @end  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SqliteInterface.m  #import "SqliteInterface.h" #import "CommonDefines.h"  @implementation SqliteInterface  @synthesize dbRealPath, dbo;  static SqliteInterface *sharedSqliteInterface;  + (SqliteInterface *)sharedSqliteInterface {      if (!sharedSqliteInterface) {          sharedSqliteInterface = [[SqliteInterface alloc] init];      }      return sharedSqliteInterface; }  - (void) connectDB {      if (dbo == nil) {          dbo = [[FMDatabase alloc] initWithPath:dbRealPath];          if (! [dbo open]) {              NSLog(@"Could not open database.");          }      }else {          NSLog(@"Database has already opened.");      } }  - (void) closeDB {      [dbo close];      BR_RELEASE(dbo); }  - (void) setupDB:(NSString *)dbFileName {      if (dbFileName == nil) {          return;      }       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);      NSString *documentsPath = [paths objectAtIndex:0];      NSFileManager *fileManager = [NSFileManager defaultManager];      NSError *err;       dbRealPath = [documentsPath stringByAppendingString:[NSString stringWithFormat:@"/%@",dbFileName]];       if (![fileManager fileExistsAtPath:dbRealPath]) {          NSString *dbSrcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFileName];          BOOL copySuccess = [fileManager copyItemAtPath:dbSrcPath toPath:dbRealPath error:&err];           if (!copySuccess) {              NSLog(@"Failed to copy database '%@'.", [err localizedDescription]);          }      }      NSLog(@"dbRealPath:%@",dbRealPath); } @end //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// AppDelege.m  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       // Override point for customization after application launch.       [[SqliteInterface sharedSqliteInterface] setupDB: @"xxxxx.sqlite"];      [[SqliteInterface sharedSqliteInterface] connectDB]; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Finally, remember to add libsqlite3.dylib. Then add the corresponding object and model to operate the database.  #import "MarkedPageModel.h" #import "SqliteInterface.h" #import "CommonDefines.h"  @implementation MarkedPageModel  - (NSMutableArray *)getMarkedPages {      FMDatabase *dbo = [SqliteInterface sharedSqliteInterface].dbo;      NSMutableArray *resultArray = nil;       NSString *selectSql = @"SELECT * FROM markedPage";      FMResultSet *rs = [dbo executeQuery:selectSql];       while ([rs next]) {          if (resultArray == nil) {              resultArray = [[[NSMutableArray alloc] init] autorelease];          }          MarkedPageObject *markedPageObject = [[MarkedPageObject alloc] init];          markedPageObject.markedPage = [rs intForColumn:@"markedPage"];          markedPageObject.markedTime = [rs stringForColumn:@"markedTime"];          markedPageObject.isMarked = [rs boolForColumn:@"isMarked"];          [resultArray addObject:markedPageObject];          BR_RELEASE(markedPageObject);      }      [rs close];      return resultArray; }  - (BOOL)AddMarkedPage:(int)page             MarkedTime:(NSString *)time               isMarked: (BOOL)marked {      FMDatabase *dbo = [SqliteInterface sharedSqliteInterface].dbo;      NSString *selectSql = @"INSERT INTO markedPage (markedPage,markedTime,isMarked) VALUES (?,?,?)";      return [dbo executeUpdate:selectSql            withArgumentsInArray:[NSArray arrayWithObjects:                                  [NSNumber numberWithInt:page], time, [NSNumber numberWithBool:marked],nil]]; }  - (BOOL)deleteMarkedPage:(int)page {      FMDatabase *dbo = [[SqliteInterface sharedSqliteInterface] dbo];      NSString *deleteSql = @"DELETE FROM markedPage WHERE markedPage = ?";      return [dbo executeUpdate:deleteSql withArgumentsInArray:[NSArray arrayWithObjects:                                                                [NSNumber numberWithInt:page], nil]]; }  @end
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.