iOS basic use of Fmdb

Source: Internet
Author: User
Tags sqlite database

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

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.