Forward: Http://wenku.baidu.com/link?url=LSPSZSPxN4pVwWNwqEXSoY0-jlnXq-_14C7qV1FV9_gFIMPjdKlXrG4Nrh_ 08ezs10icym3syaulljwj0djvojepa8hl9rq1gvzv50yz7bi
Fmdb is a third-party framework for data storage, and it has many advantages over sqlite and core data.
Fmdb is object-oriented, it encapsulates the C language API of SQLite in OC Way, it is more convenient to use, and does not require much knowledge of database operation. But it also has some problems, such as cross-platform, because it is packaged in OC language, so it can only be used in iOS development, if you want to achieve cross-platform operation, to reduce the cost of development and maintenance costs, you need to use the comparison of the original SQLite.
Core data is an ORM embodiment, using core data need to use the transformation of the model, although the operation is simple, do not need to directly manipulate the database, but performance is not directly using SQLite high. But SQLite needs to use the C language function, the operation is more troublesome, so it needs to be encapsulated. But if it is simply encapsulated, it is likely to overlook many important details, such as how to handle concurrency and security issues.
Therefore, it is recommended to use the third-party framework Fmdb, which is the encapsulation of the LIBSQLITE3 framework, the use of steps similar to SQLite, and it is a multi-threaded simultaneous operation of a table when processed, it means that it is thread-safe. Fmdb is a lightweight framework that is flexible to use and is the first choice for many enterprise development.
Important classes in the Fmdb
Fmdatabase: A Fmdatabase object represents a separate SQLite database for executing SQL statements
Fmresultset: Result set after query execution with Fmdatabase
Fmdatabasequeue: Used to execute multiple queries or updates in multiple threads, it is thread-safe
FMDB Use steps
1. Download the Fmdb file Fmdb to add the Fmdb folder to your project
2. Import the SQLite framework and import the FMDatabase.h file
3. Similar to SQLite use steps, you need to get the database file path, then get the database, open the database, then the database to operate, and finally close the database. The code looks like this:
1.//VIEWCONTROLLER.M
2.//Jrfmdb
3.//
4.//Created by jerehedu on 15/6/18.
5.//Copyright (c) jerehedu 2015. All rights reserved.
6.//
- 7.
8. #Import "ViewController.h"
9. #Import "FMDatabase.h"
- @interface Viewcontroller ()
- @property (nonatomic, strong) Fmdatabase *db;
- @end
- @implementation Viewcontroller
- -(void) viewdidload
- {
- [Super Viewdidload];
- Import the SQLite framework and import the Fmdb folder
- 1. Get the path to the database file
- NSString *doc=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
- NSString *filename=[doc stringbyappendingpathcomponent:@ "Student.sqlite"];
- NSLog (@ "filename =%@", filename);
- 2. Access to the database
- Fmdatabase *db = [Fmdatabase databasewithpath:filename];
- 3. Open the Database
- if ([DB Open]) {
- NSLog (@ "OK");
- 4. Create a Watch
- BOOL result=[db executeupdate:@ "CREATE TABLE IF not EXISTS t_student (ID integer PRIMARY KEY autoincrement, Name text not NULL, age integer Not NULL); "];
- if (Result) {
- NSLog (@ "Genesis success");
- }Else{
- NSLog (@ "Failure to create a table");
- }
- }
- self.db=db;
- Inserting data
- [Self insertstu];
- [Self deletestu:6];
- [Self updatestu:@ "apple7_name": @ "7777"];
- [Self querystu];
- [Self dropstu];
- [Self insertstu];
- [Self querystu];
- 6. Close the database
- [Self.db Close];
- }
- #pragma mark inserts data
- -(void) Insertstu
- {
- for (int i=0; i<10; i++)
- {
- NSString *name = [NSString stringwithformat:@ "1apple%i_name", I];
- int age = arc4random ()%3+20;
- 1. Executeupdate: An indeterminate parameter is used for the placeholder (the following parameters must all be OC objects)
- [Self.db executeupdate:@ INSERT into t_student (name, age) VALUES (?,?); ', name,@ (age)];
- 2. Executeupdatewithformat: Indeterminate parameters are occupied with%@,%d, etc. (the parameter is the original data type)
- [Self.db executeupdatewithformat:@ "insert into t_student (name, age) VALUES (%@,%i);", Name,age];
- 3. Array
- [Self.db executeupdate:@ INSERT into t_student (name, age) VALUES (?,?); ' withargumentsinarray:@[name,@ (age)];
- }
- }
- #pragma mark Delete data
- -(void) Deletestu: (int) idnum
- {
- A. Executeupdate: Indeterminate parameters are used to occupy bits (the following parameters must all be OC objects)
- [Self.db executeupdate:@ "Delete from t_student where id=?;", @ (Idnum)];
- B. Executeupdatewithformat: Indeterminate parameters are occupied by%@,%d, etc.
- [Self.db executeupdatewithformat:@ "Delete from t_student where name=%@;", @ "Apple9_name"];
- }
- #pragma mark destroys the form
- -(void) Dropstu
- {
- [Self.db executeupdate:@ "drop table if exists t_student;];
- 4. Create a Watch
- BOOL result=[self.db executeupdate:@ "CREATE TABLE IF not EXISTS t_student (ID integer PRIMARY KEY autoincrement, Name text Not NULL, the age integer is not null); "];
- if (Result) {
- NSLog (@ "Re-create TABLE success");
- }Else{
- NSLog (@ "Failed to create a table again");
- }
- }
- #pragma mark to modify the data
- -(void) Updatestu: (NSString *) Oldname:(nsstring*) newName
- {
- [Self.db executeupdatewithformat:@ "Update t_student set name=%@ where name=%@;", Newname,oldname];
- [Self.db executeupdate:@ "Update t_student set name=? Where Name=?", Newname,oldname];
- }
- #pragma mark queries the data
- -(void) Querystu
- {
- 1. Execute the query statement
- Fmresultset *resultset = [self.db executequery:@ "select * from T_student;];
- Fmresultset *resultset = [self.db executequery:@ "select * from T_student where id<?;", @ (14)];
- 2. Iterating through the result set
- while ([ResultSet next]) {
- int idnum = [resultSet intforcolumn:@ "id"];
- NSString *name = [resultSet objectforcolumnname:@ "name"];
- int age = [ResultSet intforcolumn:@ ' age '];
- NSLog (@ "id=%i, name=%@, age=%i", idnum,name,age);
- }
- }
- -(void) didreceivememorywarning
- {
- [Super didreceivememorywarning];
- Dispose of any resources the can be recreated.
- }
- @end
Iosfmdb and CoreData