iOS development--Working with databases

Source: Internet
Author: User

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 ')

    1. Find all: SELECT * FROM tablename
    2. Find a single attribute: select name from tablename
    3. Find multiple properties: Select ID, name from tablename
    4. Search by certain criteria: Select ID from tablename where ID =xx
    5. Find by multiple criteria: Select ID from tablename where ID =xx and name = XX
    6. Search for a specific string in the condition: Select ID from tablename where name like '%xx% '

(4) Simple Where condition

    1. = equals
    2. > Greater than
    3. < less than
    4. >= greater than or equal to
    5. <= less than or equal to
    6. <> Not equal to
    7. !> not greater than
    8. % 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

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.