iOS development code implements database Fmdb operations 1. Introduction
Requirements: If you need to save a large amount of complex data, use a database, such as a compliance test project
Commonly used databases:
(1) Microsoft SQL Server 2000/2008, SMB uses more
(2) Oracle is more complex and large enterprises use more
(3) MySQL database, web site use more
(4) SQLite: Local database, access to data fast enough, direct access to files
Simple enough, the functionality is not particularly complete with respect to other database software, enough
Small enough that the system does not exceed 1M and is suitable for use on the mobile side
2.FMDB Operating database
(1) configuration
Import files,
Add a binary library libsqlite3.dylib,
Contains the header file #import "FMDatabase.h"
3.database used in projects- singleton design pattern
- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //(1) Create a database[self creatandinitdatabase]; //(2) Creating a data table[self createtable]; //(3) inserting data[self insertdata]; //(4) query data[self querydata]; //(5) Modify and delete data//reference: Inserting data}-(void) querydata{//Show everyone's informationNSString *sql =@"SELECT * from Studentinfo"; //Fmresultset represents the result set after queryFmresultset *resultset =[_database Executequery:sql]; // while([ResultSet next]) {NSLog (@"sid =%@,username =%@, password =%@,score =%@", [ResultSet Stringforcolumn:@"Sid"],[resultset Stringforcolumn:@"username"],[resultset Stringforcolumn:@"Password"],[resultset Stringforcolumn:@"score"]); } }-(void) insertdata{intSid =1501; NSString*username =@"Zhangsan"; NSString*password =@"123"; NSString*score =@" -"; NSString*sql =@"INSERT INTO Studentinfo (Sid,username,password,score) VALUES (?,?,?,?)"; BOOL b= [_database executeupdate:sql,[nsstring stringWithFormat:@"%d", Sid],username,password,score]; NSLog (@"B =%d", b);}-(void) createtable{NSString*sql=@"CREATE table if not exists studentinfo (SID integer,username varchar), password varchar, score varchar (20)) "; //ExecuteQuery used to execute a SELECT statementBOOL b=[_database Executeupdate:sql]; NSLog (@"b=%d", b);}-(void) creatandinitdatabase{//iOS Security-sandbox//(1). Each app content is placed under a sandbox directory//(2). Each app can only modify files in the sandbox directory, other application files cannot be modified//(3). Default Folder Documents,library,tmp//development: Files created by yourself are placed under documents//Determine file location//Current folder: Nshomedirectory ()//NSLog (@ "home =%@", nshomedirectory ());//NSLog (@ "%@", [[Nsfilemanager Defaultmanager] Contentsofdirectoryatpath:nshomedirectory () Error:nil]); //Set PathNSString *path = [NSString stringWithFormat:@"%@/documents/stuinfo.sqlite", Nshomedirectory ()]; //Create the database (open if it does not exist, open if it exists)_database =[[Fmdatabase alloc] initwithpath:path]; if(![_database Open]) {NSLog (@"Open Failed"); return; } NSLog (@"Open Success"); }
4. Display results
iOS development using code to implement database Fmdb operations