iOS development--Use the Fmdb database to introduce requirements:
If you need to save a large number of complex data structures, use a database, such as a pass-through exam project
1, the basic introduction of the database
A database (db) is a data warehouse in which a data model is organized and stored for storage management. It is developed by file management, and today's databases are basically relational databases.
The basic operation of the database is to increase, delete, check and change.
Several common databases, Oracle,access,sql Server,db2,mysql
The database used on the phone is SQLite, because it occupies a good resource, easy to configure and other reasons.
2, Mesasqlite use
Example: Using a data store to store information about a class student
School Number: SID User name: username password: Password score: score 1501 zhangs 123 321 1502 Heihei 90
(1) Create a database
You can create a new database by selecting New from the menu bar directly.
(2) Creating a data table
The data table is created a little more, you can click on the + sign in structure to create. Then
Enter create statement in SQL query creation table tablename (ID type, name type) (enter directly in the input box below)
(3) SQL Structured Query statements
First, you must have data, add data to the INSERT INTO TableName (Id,name) VALUES (one, ' Zhang ')
- Find all: SELECT * FROM tablename
- Find a single attribute: select name from tablename
- Find multiple properties: Select ID, name from tablename
- Search by certain criteria: Select ID from tablename where ID =xx
- Find by multiple criteria: Select ID from tablename where ID =xx and name = XX
- Search for a specific string in the condition: Select ID from tablename where name like '%xx% '
(4) Simple Where condition
- = equals
- > Greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
- <> Not equal to
- !> not greater than
- % matching string% matching string
3. Using Fmdb in iOS development
FMDB is an open source third-party framework.
Fmdatabase-Represents a separate database that is used to execute SQLite commands.
Fmresultset-Represents the result set that is returned after Fmdatabase executes.
Fmdatabasequere-is used in multiple threads when using this class.
(1) Import Fmdb
Load directly into the project and add the Sqlite3.dylib binary library (find SQLite).
In the required file, add the header file #import "FMDatabase.h"
(2) Create and open a database
NSString *path = [[NSBundle mainbundle] Pathforresource:@ "data.sqlite" Oftype:nil]; = [[Fmdatabase alloc] initwithpath:path]; if (! _database.open) { NSLog (@ " Open failed "); } Else { NSLog (@ " success "); }
(3) query operation, and get return set
NSString *sql =@"SELECT * from Firstlevel"; Fmresultset*resultset =[_database Executequery:sql]; Nsmutablearray*marray =[[Nsmutablearray alloc] init]; while([resultset next]) {Model*model =[[Model alloc] init]; Model.ID= [ResultSet stringforcolumn:@"ID"]; Model.name= [ResultSet stringforcolumn:@"name"]; [Marray Addobject:model];}
Conclusion: refueling every day ...
iOS development--Working with databases