A brief description and introduction of Fmdb
Fmdb's GitHub address Https://github.com/ccgus/fmdb
Fmdb is a concise, easy-to-use packaging library, a brief introduction of Fmdb
After Fmdb downloads the file, the project must import the following files and use the LIBSQLITE3.0.TBD dependency package
The Fmdb is compatible with both arc and non-ARC projects and automatically adjusts the associated memory management code according to the engineering configuration.
Two. Core class of Fmdb
Fmdb has three main classes
(1) Fmdatabase
A Fmdatabase object represents a separate SQLite database for executing SQL statements
(2) Fmresultset
Use Fmdatabase to execute a query result set (that is, the collection of query results)
(3) Fmdatabasequeue
Used to execute multiple queries or updates in multiple threads, it is thread-safe
Three. Open the database
Creates a Fmdatabase object from the specified SQLite database file path
path for the database you are creating
Fmdatabase *db = [Fmdatabase Databasewithpath:path];
if (![ DB Open]) {
NSLog (@ "Database open failed");
}
There are three types of file paths
(1) Specific file path
Automatically created if it does not exist
(2) empty string @ ""
An empty database is created in the temp directory
The database file is also deleted when the Fmdatabase connection is closed
(3) Nil
Creates a memory Ching MS database, and the database is destroyed when the Fmdatabase connection is closed
Iv. Performing Updates
In Fmdb, all operations except queries are called updates
Create, DROP, insert, UPDATE, delete, and more
To perform an update using the Executeupdate: method
Example
[DB executeupdate:@ "UPDATE t_student SET age =? WHERE name =?; ", @20, @" Jack "]
Five. Execute the query
Query method
-(Fmresultset *) ExecuteQuery: (nsstring*) SQL, ...
-(Fmresultset *) Executequerywithformat: (nsstring*) format, ...
-(Fmresultset *) ExecuteQuery: (NSString *) SQL Withargumentsinarray: (Nsarray *) arguments
Sample code
//
Viewcontroller.m
Database
copyright©2016 year ZM. All rights reserved.
//
#import "ViewController.h"
#import "FMDB.h"
@interface viewcontroller ()
@property (nonatomic,strong)fmdatabase *db;
@end
@implementation Viewcontroller
-(void) viewdidload {
[Super viewdidload];
//1. get the path to the database file
nsstring *doc = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES) lastobject];
nsstring *filename= [Doc stringbyappendingpathcomponent:@ "Student.sqlite"];
//2. Get Database
fmdatabase *db = [fmdatabase databasewithpath: fileName];
//3. Open Database
if ([DB open]) {
// Create a watch
BOOL result = [db executeupdate:@ "CREATE table if not exists t_student (ID integer PRIMARY key aut Oincrement,name text not null,age integer NOT null); " ];
if (result) {
NSLog(@ " CREATE table succeeded ");
}Else{
NSLog(@ " failed to create table ");
}
}
self. DB = db;
}
-(void) Touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent *) event{
[self delete];
[self insert];
[self query];
}
Delete Data
-(void) delete{
[self. DB executeupdate:@ "drop table if exests t_student;" ];
[self. DB executeupdate:@ "CREATE table if not exists t_student (ID integer primary key autoincrement,name Tex T is not null, the age integer is not null); ];
}
Inserting Data
-(void) insert{
for (int i = 0; i < i++) {
nsstring *name = [nsstring stringwithformat:@ "jack-%d",arc4random_uniform (+)];
[self. DB executeupdate:@ "INSERT into t_student (name,age) values (?,?);" , Name,@ (arc4random_uniform)];
}
}
Enquiry
-(void) query{
//1. Execute Query Statement
fmresultset *resultset = [self. DB executeQuery:@ "SELECT * from T_student"];
//2 Traversal results
while ([ResultSet next]) {
int id = [ResultSet intforcolumn:@ "id"];
nsstring *name = [ResultSet stringforcolumn:@ "name"];
int Age = [ResultSet intforcolumn:@ ' age '];
NSLog(@ "%d%@%d", id,name,age);
}
}
@end
IOS database simple use of third-party Fmdb