Fmdb Common methods

Source: Internet
Author: User

Instantiate Fmdatabase
//paths:ios under document PATH, Document is a read-write folder in iOS, in document, you can view the database by printing the sandbox path  nsarray  *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES  ); nsstring  *documentdirectory = [Paths Objectatindex:0 ]; //dbpath: Database path  nsstring  *dbpath = [Documentdirectory stringbyappendingpathcomponent:@ "test.db" ]; //Create db instance, SQLite automatically creates "db_name.db"  if there are no "db_name.db" files in the path Fmdatabase *db= [Fmdatabase Databasewithpath:dbpath]; if  (! [DB Open]) {nslog  (@ "Database open failed!! "); return ;}  
Fmdb Common methods
#import <Foundation/Foundation.h>   #import "FMDatabase.h"   #import "FMDatabaseAdditions.h"    @interface widbroot : nsobject   @property(Retain,nonatomic) Fmdatabase *db;@property(Retain,nonatomic)NSString*dbname;//+ (ID) modelwithdbname: (NSString *) dbName; - (ID) Initwithdbname: (NSString*) DbName;//Delete database- (void) Deletedatabse;//Database storage path//-(NSString *) GetPath: (NSString *) dbName; //Open Database- (void) Readydatabse;//Determine if a table exists- (BOOL) Istableok: (NSString*) TableName;//Gets the number of data bars in the table- (BOOL) Gettableitemcount: (NSString*) TableName;//Create a table- (BOOL) CreateTable: (NSString*) TableName witharguments: (NSString*) arguments;//Delete table-delete table completely- (BOOL) Deletetable: (NSString*) TableName;//Clear table-Clear data- (BOOL) Erasetable: (NSString*) TableName;//Insert Data- (BOOL) Inserttable: (NSString*) SQL, ...;//Modify Data- (BOOL) UpdateTable: (NSString*) SQL, ...;//integral type- (Nsinteger) Getdb_integerdata: (NSString*) TableName Withfieldname: (NSString*) FieldName;//Boolean type- (BOOL) Getdb_booldata: (NSString*) TableName Withfieldname: (NSString*) FieldName;//String type- (NSString*) Getdb_stringdata: (NSString*) TableName Withfieldname: (NSString*) FieldName;//Binary data type-(NSData *) Getdb_bolbdata: (NSString*) TableName Withfieldname: (NSString*) FieldName;@end  
Implementation of common methods of Fmdb
//Database storage path (internal use)- (NSString*) GetPath: (NSString*) DbName {Nsarray*paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask,YES);NSString*documentsdirectory = [Paths Objectatindex:0];return[Documentsdirectory Stringbyappendingpathcomponent:dbname]; }//Open Database- (void) Readydatabse {if([DB databaseexists])return;//db = [Fmdatabase databasewithpath:dbname]; DB = [[Fmdatabase alloc] initwithpath:dbname];if(!      [DB Open])          {[DB close]; NSAssert1 (0, @"Failed to open database file with message '%@ '.", [DB lasterrormessage]); }//Kind of experimentalish. [DB setshouldcachestatements:YES]; }//Delete database- (void) Deletedatabse {BOOLSuccessNserror*error;Nsfilemanager*filemanager = [NsfilemanagerDefaultmanager];//delete the old db.     if([FileManager Fileexistsatpath:dbname])          {[DB close]; Success = [FileManager removeitematpath:dbname error:&error];if(!success) {NSAssert1 (0, @"Failed to delete the old database file with message '%@ '.", [error localizeddescription]); }      }      }//Determine if a table exists- (BOOL) Istableok: (NSString*) TableName {fmresultset *rs = [DB executequery:@"SELECT COUNT (*) as ' count ' from sqlite_master WHERE type = ' table ' and name =?", TableName]; while([Rs Next]) {//Just print out what we ' ve got in a number of formats.         NsintegerCount = [Rs intforcolumn:@"Count"]; Wilog (@"Istableok%d", count);if(0= = count) {return NO; }Else{return YES; }      }return NO; }//Gets the number of data bars in the table- (BOOL) Gettableitemcount: (NSString*) TableName {NSString*SQLSTR = [NSStringstringwithformat:@"SELECT COUNT (*) as ' count ' from%@", TableName]; Fmresultset *rs = [DB executequery:sqlstr]; while([Rs Next]) {NsintegerCount = [Rs intforcolumn:@"Count"]; Wilog (@"Tableitemcount%d", count);returnCount }return 0; }//Create a table- (BOOL) CreateTable: (NSString*) TableName witharguments: (NSString*) Arguments {NSString*SQLSTR = [NSStringstringwithformat:@"CREATE TABLE%@ (%@)", TableName, arguments];if(!      [DB Executeupdate:sqlstr]) {Wilog (@"Create db error!");return NO; }return YES; }//Delete table- (BOOL) Deletetable: (NSString*) TableName {NSString*SQLSTR = [NSStringstringwithformat:@"DROP TABLE%@", TableName];if(!      [DB Executeupdate:sqlstr]) {Wilog (@"Delete table error!");return NO; }return YES; }//Clear Table- (BOOL) Erasetable: (NSString*) TableName {NSString*SQLSTR = [NSStringstringwithformat:@"DELETE from%@", TableName];if(!      [DB Executeupdate:sqlstr]) {Wilog (@"Erase table error!");return NO; }return YES; }//Insert Data- (BOOL) Inserttable: (NSString*) SQL, ...      {va_list args; Va_start (args, SQL);BOOLresult = [DB executeupdate:sql error:NilWithargumentsinarray:NilOrvalist:args]; Va_end (args);returnResult }//Modify Data- (BOOL) UpdateTable: (NSString*) SQL, ...      {va_list args; Va_start (args, SQL);BOOLresult = [DB executeupdate:sql error:NilWithargumentsinarray:NilOrvalist:args]; Va_end (args);returnResult }//integral type- (Nsinteger) Getdb_integerdata: (NSString*) TableName Withfieldname: (NSString*) FieldName {Nsintegerresult =NO;NSString*sql = [NSStringstringwithformat:@"Select%@ from%@", FieldName, TableName]; Fmresultset *rs = [DB executequery:sql];if([Rs Next]) result = [Rs Intforcolumnindex:0]; [Rs Close];returnResult }//Boolean type- (BOOL) Getdb_booldata: (NSString*) TableName Withfieldname: (NSString*) FieldName {BOOLResult result = [ SelfGetdb_integerdata:tablename Withfieldname:fieldname];returnResult }//String type- (NSString*) Getdb_stringdata: (NSString*) TableName Withfieldname: (NSString*) FieldName {NSString*result =NO;NSString*sql = [NSStringstringwithformat:@"Select%@ from%@", FieldName, TableName]; Fmresultset *rs = [DB executequery:sql];if([Rs Next]) result = [Rs Stringforcolumnindex:0]; [Rs Close];returnResult }//Binary data type-(NSData *) Getdb_bolbdata: (NSString*) TableName Withfieldname: (NSString*) FieldName {NSData *result =NO;NSString*sql = [NSStringstringwithformat:@"Select%@ from%@", FieldName, TableName]; Fmresultset *rs = [DB executequery:sql];if([Rs Next]) result = [Rs Dataforcolumnindex:0]; [Rs Close];returnResult }  - (void) Dealloc {[DB close];      [DB release];      [DBName release]; [SuperDealloc]; }@end  

Fmdb Common methods

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.