iOS Development Database Chapter-SQL Code application Example

Source: Internet
Author: User

First, the use of code to bulk Add (import) data into the database

1. Execute SQL statement to add a message to the database

SQL statement that inserts a single piece of data:

  

After clicking Run to execute the statement, refresh the data

2. Use code to bulk add multi-line data samples in iOS projects

code example:

1 //2 //main.m3 //01-Add multiple rows of data to the database4 //5 //Created by Apple on 14-7-26.6 //Copyright (c) 2014 wendingding. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One intMainintargcConst Char*argv[]) A { -  - @autoreleasepool { theNsarray *[email protected][@"Zhang Yi",@"Zhang Yi",@"Zhang San",@"Zhang Si"]; -Nsmutablestring *sql=[nsmutablestringstring]; -          -          for(intI=0; i< $; i++) { +             intid=i+1; -             //The warning here is an unsigned type conversion +NSString *name=Names[arc4random_uniform (Names.count)]; AName=[name Stringbyappendingformat:@"-%d", Arc4random_uniform ( $)]; at             //generates a random number with a range of 20-centered fluctuation -             intAge=arc4random_uniform ( -)+Ten; -[SQL AppendFormat:@"INSERT into T_student (id,name,age) VALUES (%d, '%@ ',%d); \ n", Id,name,age]; -         } -         //writing SQL to a file -[SQL WriteToFile:@"/users/apple/desk/students.sql"atomically:yes encoding:nsutf8stringencoding Error:nil]; inNSLog (@"\n%@", SQL); -         } to     return 0; +}

Role: Generate 200 corresponding SQL INSERT statements

Printing results are:

Using a text editor, open the generated SQL file.

These SQL statements can be copied to navicat for execution, or the entire file can be executed directly.

Create a table in the database:

Select Execute SQL file:

Click Cancel when you are finished executing.

Refresh the database to see the 200 data inserted

Second, a simple presentation of the page

Description

Use limit to precisely control the number of query results, such as querying only 10 data at a time

Format select * from table name limit value 1, value 2;

Example

SELECT * from T_student limit 4, 8;

Can be understood as: Skip the first 4 statements, then fetch 8 records

The limit is often used for paging queries, such as 5 data per page, so you should take the data

1th page: Limit 0, 5

2nd page: Limit 5, 5

3rd page: Limit 10, 5

...

Page N: Limit 5* (n-1), 5

The function of the following statement

SELECT * from t_student limit 7;

Equivalent to select * from T_student limit 0, 7; Indicates the first 7 records are taken

Third, supplementary

1. About foreign KEY constraints (establish a link between two tables)

The first approach: you can create a new relationship table, so that the previous two tables (Class table and student table to establish a corresponding link), but this approach is very redundant, there is no need

Second approach: using FOREIGN KEY constraints

One-to-one, one-to-many, many-to-many relationships. When two tables are connected, how do you set the foreign key (in which table?) )

2. About table joins

Subquery: The name of all types of Cantonese cuisine is required to be queried.

The query results are:

Connection query:

The query results are:

iOS Development Database Chapter-SQL Code application Example

Related Article

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.