A brief introduction to the Fmdb,fmdb is the SQLite database framework of the iOS platform, which encapsulates SQLite's C language API in OC mode. Easy to use, save a lot of redundant C language code, compared to Apple's own core data framework, more lightweight and flexible, provide multi-threaded secure database operation method, effectively prevent data confusion.
--------------------------------------------------------------------------------------------------------------- ----
GitHub Address
Https://github.com/ccgus/fmdb
Specific use:
1. Import the Fmdb into your project and add the LIBSQLITE3.TBD (Xcode7 later version).
2.
#import <Foundation/Foundation.h> @interface databasemanager:nsobject//Write a single case + (instancetype) sharedfmdatabase;
#import "DataBaseManager.h" #import "FMDB.h" @interface Databasemanager () @property (nonatomic,strong) fmdatabase * db;@ End@implementation databasemanager+ (instancetype) sharedfmdatabase{static databasemanager*manager=nil; Static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Manager=[[databasemanager Alloc]init]; }); Return manager;} -(instancetype) init{self = [super init]; if (self) {nsstring*doc=nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) [0];//Get the database Nsstring*filename=[doc stringbyappendingpathcomponent:@ "Students.sqlite"]; A database file is created automatically when a database file does not exist. Determine if the database exists if (!self.db) {//does not exist create self.db=[fmdatabase databasewithpath:filename]; }//Set cache for database, improve query efficiency [self.db Setshouldcachestatements:yes]; BOOL open=[self.db open];//Test whether the database is open successfully if (open) {NSLog (@ "Database open successfully"); }else{NSLog (@ "Database open failed"); } Open the database if ([self.db Open]) {if (![ Self.db tableexists:@ "T_student"]) {//CREATE TABLE BOOL result=[self.db executeupdate: @ "CREATE TABLES t_student (id INTEGER PRIMARY KEY autoincreament, Name text, age text)"]; The dictionary is converted to NSData, and the SQL is saved with the BLOB type if (result) {NSLog (@ "Success CREATE TABLE"); }else{NSLog (@ "Failure to create a table"); }} NSLog (@ "already has a table and does not need to be added again"); }//close the database [self.db close]; } return self;}
Basic operation of the data:
Add data [self.db open]; [Self.db executeupdate:@ "INSERT into T_student (name,age) VALUES (?,?)" @ "xiaoming" @ "123"]; [Self.db Close];
querying data [self.db open]; Fmresultset*resultset=[self.db executequery:@ "SELECT * from T_student"]; while ([ResultSet next]) { int id = [ResultSet intforcolumn:@ "id"]; NSString *name = [resultSet stringforcolumn:@ "name"]; nsstring* age = [ResultSet stringforcolumn:@ ' age ']; NSLog (@ "%d%@%@", ID, name, age); } [Self.db Close];
iOS basic use of Fmdb