ios-Data storage methods

Source: Internet
Author: User
Tags aliases

1 How data is stored in iOS

1> storage of small data, in the original path overlay storage

Plist (can only store nsarray\nsdictionary)
Preference (Preferences \nsuserdefaults)
Nscoding (Nskeyedarchiver\nskeyedunarchiver)

2> storing large volumes of data

SQLite3 Pure C language, lightweight, fast retrieval speed
Core Data OC Language


2 SQLite1> What is SQLite

SQLite is a lightweight, embedded database
It occupies very low resources, in the embedded device, may only need hundreds of K of memory is enough
Its processing speed is faster than the two famous databases of MySQL and PostgreSQL.


2> field type

SQLite divides the data into the following types of storage:
Integer: Integer value
Real: floating-point value
Text: literal string
BLOB: Binary data (such as files)

In fact, SQLite is untyped.
The ability to store string literals ( except primary keys ), even if declared as an integer type
It is possible to declare what type or not to declare a type when building a table, which means that the statement can be written like this:
CREATE table T_student (name, age);


3> Record Deletion form

1) Create a table

The CREATE table if not EXISTS table name (field name 1 field type 1, Field Name 2 field type 2, ...);

Eg:create table t_student (ID integer, name text, age Inetger, score real);

2) Delete Table

drop table if exists name;

Eg:drop table t_student;


4>crud (Increase and deletion check)

1) Add Data

Insert into table name (Field 1, Field 2, ...) VALUES (Value of field 1, Value of field 2, ...);

Eg:insert into T_student (name, age) VALUES (' MJ ', 10); The string contents in the database should be enclosed in single quotes

2) Delete data

Delete from table name;

Eg:delete from T_student;//The above example deletes all records in the T_student table

3) Update data

Update table name set field 1 = value of field 1, field 2 = value of field 2, ...;
Eg:update t_student Set name = ' Jack ', age = 20; Changes the name of all records in the table to Jack,age to 20

4) Find data

Select field 1, Field 2 (or *) from table name where field name = value

Eg:select name, age from T_student;
SELECT * from T_student;

5> conditional statements

If you only want to update or delete some fixed records, you must add some conditions after the DML statement
Common formats for conditional statements
where field = a value; cannot be used for two x =
The Where field is a value; is equals =
where field! = a value;
The Where field is not a value; is isn't equivalent to! =
Where field > a value;
where field 1 = a value and field 2 > a value; and equivalent to the && in C language
where field 1 = a value or field 2 = a value; or equivalent to the C language | |


6> aliases

Alias re-table connection query is useful

1) Format (both fields and tables can be aliased)
Select field 1 alias, Field 2 alias, ... from table name alias;
Select field 1 alias, Field 2 as Alias, ... from table name as alias; (aliases can be written directly behind a field, omitting as)
Select Alias. Field 1, alias. Field 2, ... from table name alias;

Eg:select name MyName, age myage from T_student;

Select S.name, s.age from T_student s; Give T_student an individual name called S and use s to refer to the fields in the table


7> calculate the number of records

Select count (field) from table name;
Select COUNT (*) from table name;
Eg:select count (age) from T_student;
Select COUNT (*) from T_student where score >= 60;


8> sort

The default is ascending

SELECT * from T_student order by age;

SELECT * from T_student ORDER by age desc ; Descending
SELECT * from T_student ORDER BY age ASC ; Ascending (default)

You can also sort by multiple fields
SELECT * from T_student ORDER BY age ASC, height desc;
Sort by age (ascending), and age equal by height (descending)


9> constraints1> Simple constraints

Not NULL: The value of the specified field cannot be null
Unique: The value of the specified field must be unique
Default: Specify a value for the field

Eg:create table t_student (ID integer, name text NOT null-unique, age integer NOT null default 1);

2> PRIMARY KEY constraints

Primary KEY (Primary key, referred to as PK) uniquely identifies a record
For example, t_student can add an ID field as the primary key, which is equivalent to a person's ID
A primary key can be a field or multiple fields, which means that a table can have more than one primary key, but preferably only 1

1) Design principles

The primary key should not be meaningful to the user
Never update the primary key
The primary key should not contain dynamically changing data
The primary key should be automatically generated by the computer

If you want the primary key to grow automatically (must be an integer type), you should increase the AutoIncrement
CREATE table t_student (ID integer primary key autoincrement, name text, age integer);

3> FOREIGN KEY constraint

The use of FOREIGN key constraints can be used to establish a table-to-table connection, usually a multi-attribute table referencing the primary key of another table.
The general case of a foreign key is a field in a table that refers to the primary key field of another table
Create a new foreign key
CREATE table t_student (ID integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_ Student_class foreign Key (class) references T_class (ID));

Constraint Fk_student_class foreign key FOREIGN KEY constraint name (Class) T_student table's class field Referencest_class (ID) Another table's primary key field

There is a foreign key called fk_t_student_class_id_t_class_id in the T_student table.
The role of this foreign key is to refer to the ID field of the T_class table using the class_id field in the T_student table

The FOREIGN KEY constraint also has the cascade function


10> Table Connection Query

1) Nested query

SELECT ID from t_food_type WHERE name = ' Cantonese '; Find the ID of the Cantonese cuisine first select * from T_food where food_type_id = (SELECT ID from t_food_type where name = ' Cantonese cuisine ');//find information on all Cantonese cuisine by the ID of Cantonese cuisine

2) Multi-table query

SELECT * from T_food, food_type_id; Will query out the contents of the Cartesian product of two tables//We need to filter the same information on both sides of the ID (need to alias) SELECT * from T_food f,t_food_type tf WHERE f.food_type_id = tf.id// Continue to filter the information of Cantonese select * from T_food f,t_food_type tf WHERE f.food_type_id = tf.id and tf.name = ' Cantonese ';


11>limit

Use limit to precisely control the number of query results, such as querying only 10 data at a time
SELECT * from table name limit value 1, value 2;

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

eg

SELECT * from T_student limit 4, 8, can be understood as: Skip the first 4 statements, then take 8 records SELECT * from t_student limit 7; equivalent to select * from T_student Lim It 0, 7, means taking the top 7 records


ios-Data storage methods

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.