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