Use of IOS Fmdb

Source: Internet
Author: User

Brief introduction:

SQLite (http://www.sqlite.org/docs.html) is a lightweight relational database. The IOS SDK supports SQLite very early, and when used, it only needs to join Libsqlite3.dylib dependencies and introduce sqlite3.h header files. However, the native SQLite API is quite unfriendly in use and inconvenient when used. As a result, there is a series of libraries that encapsulate the SQLite API in the open source community, while FMDB (Https://github.com/ccgus/fmdb) is a great person in the open source community.

Use: (Bestaccountsmanager for Account data management classification, Bestaccountitem for account model)

Static Fmdatabase *_db;
static Bestaccountsmanager *sharedmanager = nil;
Single case
+ (Instancetype) Sharedmanager {
@synchronized (self) {
if (Sharedmanager = = nil) {
Sharedmanager = [[Bestaccountsmanager alloc] init];
}
}
return sharedmanager;
}

Open Database

-(void) Openaccountmanager {
NSString *path = [[Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Accountmanager.db"];
_db = [Fmdatabase Databasewithpath:path];

if ([_db Open]) {
BOOL res = [[Bestaccountsmanager Sharedmanager] createtable];

if (!res) {
NSLog (@ "CREATE table Accounts failed!");
}
}
}

Create a database
-(BOOL) createtable {
NSString *sql = [NSString stringwithformat:@ "CREATE TABLE IF not EXISTS ' account ' (' id ' PRIMARY KEY autoincrement not NULL, ' UserName ' text not null, ' Password ' text is not null, ' Host ' text is not null '];

return [_db Executeupdate:sql];
}


Increase
-(void) Insertaccountitem: (Bestaccountitem *) Item {
if ([_db Open]) {
NSString *sql = [NSString stringwithformat:@ "INSERT into ' account ' (' UserName ', ' Password ', ' Host ') VALUES ('%@ ', '%@ ', '% @ ') ", Item.username, Item.password, Item.host];
BOOL res = [_db executeupdate:sql];
if (res) {
NSLog (@ "Insert account item succeed!");
} else {
NSLog (@ "Insert account item failed!");
}
}
[_db Close];
}

By deleting
-(void) Deleteaccountitem: (Bestaccountitem *) Item {
if ([_db Open]) {
NSString *sql = [NSString stringwithformat:@ "DELETE from ' account ' WHERE ' UserName ' = '%@ ' and ' Host ' = '%@ '", Item.userna Me, Item.host];
BOOL res = [_db executeupdate:sql];
if (res) {
NSLog (@ "Delete account item succeed!");
} else {
NSLog (@ "Delete account item failed!");
}
}
[_db Close];
}

Change
-(void) Updataaccountitem: (Bestaccountitem *) Item {
if ([_db Open]) {
NSString *sql = [NSString stringwithformat:@ "UPDATE ' account ' SET ' Password ' = '%@ ' WHERE ' UserName ' = '%@ ' and ' Host ' = ' %@ ' ", Item.password, Item.username, Item.host];
BOOL res = [_db executeupdate:sql];
if (res) {
NSLog (@ "Update account item succeed!");
} else {
NSLog (@ "Update account item failed!");
}
}
[_db Close];
}

Check
-(Nsarray *) Queryaccountitems {
Nsmutablearray *accounts = [Nsmutablearray array];
if ([_db Open]) {
NSString *sql = [NSString stringwithformat:@ "select * from ' account '"];
Fmresultset *set = [_db executequery:sql];
while ([Set next]) {
Bestaccountitem *item = [[Bestaccountitem alloc] init];
Item.username = [Set stringforcolumn:@ "UserName"];
Item.password = [Set stringforcolumn:@ "password"];
Item.host = [Set stringforcolumn:@ "host"];
[Accounts Addobject:item];
}
}
[_db Close];

return accounts;
}

Use of IOS 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.