The Fmdb database framework is used

Source: Internet
Author: User

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

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.