IOS sqllite Action (i)

Source: Internet
Author: User

Common ways for iOS app data Access XML Property list--plist Nskeyedarchiver archive Preference (preferences) ·SQLite3Core DATA&NBSP;DDL statement • CREATE TABLE table name (Field name 1 field type 1, Field Name 2 field type 2, ...); The CREATE table if not EXISTS table name (field name 1 field type 1, Field Name 2 field type 2, ...); CREATE table T_person (ID integer, name text, age Inetger),   Delete table name, drop tables t_person;  field type sqlite data The storage of values is divided into the following storage types: null: Indicates that the value is a NULL value integer: unsigned integer value real: floating-point value text: text string blob: binary data (such as file)   actually SQLite is untyped, It is possible to declare or not declare a type when building a table, which means that the CREATE TABLE statement can be written like this: Create TABLE t_student (name, age);   Note: In order to maintain good programming standards, to facilitate communication between programmers, The best way to write a table statement is to add the specific type of each field &NBSP;&NBSP;DML statement • Insert data (insert) INSERT INTO table name (Field 1, Field 2, ...) VALUES (Value of field 1, Value of field 2, ...); INSERT into T_person (the name, age) VALUES (' MJ ', 10); • Note: The string contents in the database should be enclosed in single quotation marks '   update data updated table name set field 1 = value of field 1, Field 2 = Value of field 2, ...; Update T_person Set name = ' Jack ', age = 20; Note: This will change the name of all records in the T_person table to jack,age change to 20  Delete data (delete) Delete from table name ;d elete from t_person; Note: This will delete all records in the T_person table    conditional Statements • If you only want to update or delete some fixed records, you must add some conditions after the DML statement · The format of the conditional statement is as follows: where field = a value and field > a value; //and equivalent to &&where field = some value OR field = a value;  //or equivalent to | |   Instances • Change the age of older than 10 and the name does not equal Jack to 5update T_person set age = 5 where age > and name! = ' Jack '; • Delete a person who is less than or equal to 10 or older than 30 Record Delete from T_person where the age <= or the age > 30;&NBSP;DQL Statement DQL statement is the query statement in the following format: Select field 1, Field 2, ... from table name; Select NA Me, age from t_peron;  if you want to query all fields can be used: SELECT * from table name; select * FROM t_person;  can also add conditional statement SELECT * FROM T_person w Here Age > 10;  counts the number of records you can count (field) or COUNT (*) SELECT COUNT (*) from T_person;select count (age) from T_person where Height < 1.66;  sorting • The results of the query can be sorted with order by the SELECT * from T_person the order by field; select * from T_person ORDER by AGE;&NB sp; default is sorted in ascending order (from small to large), or to descending (from large to small) select * from T_person order BY age Desc; Descending select * from T_person ORDER BY age asc; //Ascending (default)   can also be sorted with multiple fields select * from T_person ORDER by age Asc,he ight desc; Sort by age (ascending), age equal by height (descending)   Limit query number select * FROM table name limit value 1, value 2;select * from T_person limit 4, 8; above statement, You can understand this: • Skip the top 4 statements and Fetch 8 records   limitOften used to do paging query, such as 6 per page fixed display data, then each page should be the data: 1th: Limit 0, 6 2nd: Limit 6, 6 3rd page: Limit 12, 6 ... Page N: Limit 6* (n-1), 6 select * from T_person limit 7; represents the first 7 records   simple constraints • When building a table, you can set some constraints on the specific fields 1: with not NULL the value of the specified field cannot be null2: The value of the field specified by unique must be unique 3: Specify the default value for the field by using Defaults  create TABLE t_student (ID integer, name text not NULL Unique, age integer NOT null default 1), 1:name field cannot be null, and unique 2:age field cannot be null, and default is 1  PRIMARY KEY constraint (PK) • When T_ When the name attribute of some records in person is the same as the Age property, then the data cannot be distinguished, resulting in database records being not unique, which makes it inconvenient to manage the data • Good database programming specifications should be to ensure uniqueness of each record, for which a primary key constraint is added   A primary key is used to uniquely identify a record, which can be a field or multiple fields. • For example, T_person can add an ID field as the primary key, which is equivalent to a person's identity card   Primary KEY Design principle: 1: The primary key should be meaningless to the user 2: Never update the primary key 3: The primary key should not contain dynamically changing data 4: The primary key should be automatically generated by the computer   at the time of the creation of the table with primary key to declare a primary key CREATE table T_ Student (ID integer PRIMARY key,name text,age integer),   as long as PRIMARY key is declared as a primary key field, the primary key field defaults to include not NULL and a unique two Bundle   If you want the primary key to grow automatically (integer type), you should increase the Autoincrementcreate TABLE t_student (ID integer PRIMARY key autoincrement,name text , age integer);  foreign KEY constraint (FK) • Foreign KEY constraint can be used to establish the connection between table and tables • Create a new foreign key create table T_studENT (ID integer PRIMARY KEY autoincrement,name text,age integer,book_id integer REFERENCES t_book (id));  t_ The student table has a foreign key book_id that references T_book's primary key id     

IOS sqllite Action (i)

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.