iOS operations database, SQLite3 and CoreData are two very good choices, but for those of us who have mastered other database languages, it would be inconvenient to use both of these operations, SQLite3 is too complex to use, but the encapsulation is too dead when using CoreData. We need some of our own database statements, and Fmdb is a very good choice.
Here is the basic use of Fmdb
Add 4 buttons in Main.storyboard (INSERT, UPDATE, delete, query)
In the Viewcontroller
//
Viewcontroller.m
//
//
#import "ViewController.h"
#import "FMDB.h"
@interface Viewcontroller ()
@property (nonatomic, strong) Fmdatabase *db;
-(ibaction) insert;
-(ibaction) update;
-(ibaction) Delete;
-(ibaction) query;
@end
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
0. Get the database file name in the sandbox
NSString *filename = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Student.sqlite"];
1. Creating a DB Instance Object
self.db = [Fmdatabase databasewithpath:filename];
2. Open the Database
if ([Self.db Open]) {
NSLog (@ "Database open successfully");
Create a Watch
BOOL result = [self.db executeupdate:@ "CREATE table if not exists t_student (ID integer primary key autoincrement, name Te XT, age integer); "];
if (result) {
NSLog (@ "Genesis success");
} else {
NSLog (@ "Failure to create a table");
}
} else {
NSLog (@ "Database open failed");
}
}
-(ibaction) insert
{
for (int i = 0; i<40; i++) {
NSString *name = [NSString stringwithformat:@ "rose-%d", Arc4random ()% 1000];
NSNumber *age = @ (arc4random ()% 100 + 1);
[Self.db executeupdate:@ "insert into t_student (name, age) VALUES (?,?); ', name, age];
}
}
-(ibaction) update
{
[Self.db executeupdate:@ "Update t_student set age =? where name =?;", @20, @ "Jack"];
}
-(ibaction) Delete
{
}
-(ibaction) query
{
1. Querying data
Fmresultset *rs = [self.db executequery:@ "select * from T_student where >?;;", @50];
2. Traverse the result set
while (Rs.next) {
int id = [RS intforcolumn:@ "id"];
NSString *name = [rs stringforcolumn:@ "name"];
int age = [rs intforcolumn:@ ' age '];
NSLog (@ "%d%@%d", ID, name, age);
}
}
@end
The Fmdb database framework is used