欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)中使用SQL語句操作數(shù)據(jù)庫的基本用法指南

 更新時間:2015年12月08日 09:23:14   作者:文頂頂  
這篇文章主要介紹了iOS開發(fā)中使用SQL語句操作數(shù)據(jù)庫的基本用法指南,包括一些常用SQL語句的整理,需要的朋友可以參考下

SQL代碼應(yīng)用示例

一、使用代碼的方式批量添加(導(dǎo)入)數(shù)據(jù)到數(shù)據(jù)庫中

1.執(zhí)行SQL語句在數(shù)據(jù)庫中添加一條信息

201512891316441.png (314×588)

插入一條數(shù)據(jù)的sql語句:

201512891344198.png (462×149)

點(diǎn)擊run執(zhí)行語句之后,刷新數(shù)據(jù)

201512891408322.png (520×83)

2.在ios項(xiàng)目中使用代碼批量添加多行數(shù)據(jù)示例

  代碼示例:

復(fù)制代碼 代碼如下:

//
//  main.m
//  01-為數(shù)據(jù)庫添加多行數(shù)據(jù)
//
//  Created by apple on 14-7-26.
//  Copyright (c) 2014年 wendingding. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSArray *names=@[@"張一",@"張二",@"張三",@"張四"];
        NSMutableString *sql=[NSMutableString string];
       
        for (int i=0; i<200; i++) {
            int ID=i+1;
            //這里的警告為無符號類型轉(zhuǎn)換
            NSString *name=names[arc4random_uniform(names.count)];
            name=[name stringByAppendingFormat:@"-%d",arc4random_uniform(200)];
            //生成隨機(jī)數(shù),范圍以20為中心上下波動10
            int age=arc4random_uniform(20)+10;
            [sql appendFormat:@"INSERT INTO t_student (id,name,age) VALUES (%d,'%@',%d);\n",ID,name,age];
        }
        //把sql寫入到文件中
        [sql writeToFile:@"/Users/apple/Desk/students.sql" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"\n%@",sql);
        }
    return 0;
}


作用:生成200條相應(yīng)的sql插入語句

打印結(jié)果為:

201512891425048.png (918×276)

使用文本編輯器,打開生成的sql文件。

201512891444998.png (448×468)

可以把這些SQL語句拷貝到Navicat中進(jìn)行執(zhí)行,也可以直接執(zhí)行整個文件。

在數(shù)據(jù)庫中創(chuàng)建一張表:

201512891502056.png (707×302)

選擇執(zhí)行SQL文件:

201512891519654.png (336×547)

執(zhí)行完畢后,點(diǎn)擊cancel。

201512891534016.png (451×302)

刷新數(shù)據(jù)庫,查看插入的200條數(shù)據(jù)

201512891553582.png (326×161)

二、分頁簡單演示

201512891611586.png (672×346)

說明:

使用limit可以精確地控制查詢結(jié)果的數(shù)量,比如每次只查詢10條數(shù)據(jù)

格式 select * from 表名 limit 數(shù)值1, 數(shù)值2 ;

示例

select * from t_student limit 4, 8 ;

可以理解為:跳過最前面4條語句,然后取8條記錄

limit常用來做分頁查詢,比如每頁固定顯示5條數(shù)據(jù),那么應(yīng)該這樣取數(shù)據(jù)

第1頁:limit 0, 5

第2頁:limit 5, 5

第3頁:limit 10, 5

第n頁:limit 5*(n-1), 5

 

下面語句的作用

select * from t_student limit 7 ;

相當(dāng)于select * from t_student limit 0, 7 ;表示取最前面的7條記錄

 

三、補(bǔ)充

1.關(guān)于外鍵約束(建立起兩張表之間的聯(lián)系)

第一種做法:可以新建一張關(guān)系表,讓之前兩張表(班級表和學(xué)生表建立起對應(yīng)的聯(lián)系),但是這種做法很冗余,沒有必要

201512891631988.png (791×401)

第二種做法:使用外鍵約束

一對一,一對多,多對多關(guān)系。當(dāng)兩張表有聯(lián)系時,如何設(shè)置外鍵(在哪張表中設(shè)置?)

201512891650060.png (938×212)

2.關(guān)于表連接

子查詢:要求查詢所有類型為粵菜的菜名。

201512891710040.png (904×87)

查詢結(jié)果為:

201512891730238.png (467×274)

連接查詢:

201512891747208.png (1027×116)

查詢結(jié)果為:

201512891805586.png (173×378)


PS:常用的SQL語句整理
一、SQL語句

如果要在程序運(yùn)行過程中操作數(shù)據(jù)庫中的數(shù)據(jù),那得先學(xué)會使用SQL語句

1.什么是SQL

SQL(structured query language):結(jié)構(gòu)化查詢語言

SQL是一種對關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行定義和操作的語言

SQL語言簡潔,語法簡單,好學(xué)好用

 

2.什么是SQL語句

使用SQL語言編寫出來的句子\代碼,就是SQL語句

在程序運(yùn)行過程中,要想操作(增刪改查,CRUD)數(shù)據(jù)庫中的數(shù)據(jù),必須使用SQL語句

 

3.SQL語句的特點(diǎn)

不區(qū)分大小寫(比如數(shù)據(jù)庫認(rèn)為user和UsEr是一樣的)

每條語句都必須以分號 ; 結(jié)尾

 

4.SQL中的常用關(guān)鍵字有

select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等

數(shù)據(jù)庫中不可以使用關(guān)鍵字來命名表、字段

 

二、SQL語句的種類

1.數(shù)據(jù)定義語句(DDL:Data Definition Language)

包括create和drop等操作

在數(shù)據(jù)庫中創(chuàng)建新表或刪除表(create table或 drop table)

 

2.數(shù)據(jù)操作語句(DML:Data Manipulation Language)

包括insert、update、delete等操作

上面的3種操作分別用于添加、修改、刪除表中的數(shù)據(jù)

 

3.數(shù)據(jù)查詢語句(DQL:Data Query Language)

可以用于查詢獲得表中的數(shù)據(jù)

關(guān)鍵字select是DQL(也是所有SQL)用得最多的操作

其他DQL常用的關(guān)鍵字有where,order by,group by和having

 

三、基本操作

1.創(chuàng)建表

create table 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;

create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;

示例

create table t_student (id integer, name text, age integer, score real) ;

2.字段類型

SQLite將數(shù)據(jù)劃分為以下幾種存儲類型:

integer : 整型值

real : 浮點(diǎn)值

text : 文本字符串

blob : 二進(jìn)制數(shù)據(jù)(比如文件)

 

注意:實(shí)際上SQLite是無類型的,就算聲明為integer類型,還是能存儲字符串文本(主鍵除外)

建表時聲明啥類型或者不聲明類型都可以,也就意味著創(chuàng)表語句可以這么寫:

create table t_student(name, age);

 

提示:為了保持良好的編程規(guī)范、方便程序員之間的交流,編寫建表語句的時候最好加上每個字段的具體類型

 

3.刪表

格式

drop table 表名 ;

drop table if exists 表名 ;

 

示例

drop table t_student ;

 

4.插入數(shù)據(jù)(insert)

格式

insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;

示例

insert into t_student (name, age) values (‘mj', 10) ; 

注意

數(shù)據(jù)庫中的字符串內(nèi)容應(yīng)該用單引號 ' 括住

 

5.更新數(shù)據(jù)(update)

格式

update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;

示例

update t_student set name = ‘jack', age = 20 ; 

注意

上面的示例會將t_student表中所有記錄的name都改為jack,age都改為20

6.刪除數(shù)據(jù)(delete)

格式

delete from 表名 ;

示例

delete from t_student ;

注意

上面的示例會將t_student表中所有記錄都刪掉

7.條件語句

如果只想更新或者刪除某些固定的記錄,那就必須在DML語句后加上一些條件

條件語句的常見格式

where 字段 = 某個值 ;   // 不能用兩個 =

where 字段 is 某個值 ;   // is 相當(dāng)于 =

where 字段 != 某個值 ;

where 字段 is not 某個值 ;   // is not 相當(dāng)于 !=

where 字段 > 某個值 ;

where 字段1 = 某個值 and 字段2 > 某個值 ;  // and相當(dāng)于C語言中的 &&

where 字段1 = 某個值 or 字段2 = 某個值 ;  //  or 相當(dāng)于C語言中的 ||

示例

將t_student表中年齡大于10 并且 姓名不等于jack的記錄,年齡都改為 5

update t_student set age = 5 where age > 10 and name != ‘jack' ;

 

刪除t_student表中年齡小于等于10 或者 年齡大于30的記錄

delete from t_student where age <= 10 or age > 30 ;

 

猜猜下面語句的作用

update t_student set score = age where name = ‘jack' ;

將t_student表中名字等于jack的記錄,score字段的值 都改為 age字段的值

8.DQL語句

格式

select 字段1, 字段2, … from 表名 ;

select * from 表名;  // 查詢所有的字段

 

示例

select name, age from t_student ;

select * from t_student ;

select * from t_student where age > 10 ; // 條件查詢

 

9.起別名

格式(字段和表都可以起別名)

select 字段1 別名 , 字段2 別名 , … from 表名 別名 ;

select 字段1 別名, 字段2 as 別名, … from 表名 as 別名 ;

select 別名.字段1, 別名.字段2, … from 表名 別名 ;

示例

select name myname, age myage from t_student ;

給name起個叫做myname的別名,給age起個叫做myage的別名

select s.name, s.age from t_student s ;

給t_student表起個別名叫做s,利用s來引用表中的字段

 

10.計算記錄的數(shù)量

格式

select count (字段) from 表名 ;

select count ( * ) from 表名 ;

示例

select count (age) from t_student ;

select count ( * ) from t_student where score >= 60;

11.排序

查詢出來的結(jié)果可以用order by進(jìn)行排序

select * from t_student order by 字段 ;

select * from t_student order by age ;

默認(rèn)是按照升序排序(由小到大),也可以變?yōu)榻敌颍ㄓ纱蟮叫。?/p>

select * from t_student order by age desc ; //降序

select * from t_student order by age asc ;  // 升序(默認(rèn))

也可以用多個字段進(jìn)行排序

select * from t_student order by age asc, height desc ;

先按照年齡排序(升序),年齡相等就按照身高排序(降序)

 

12.limit

使用limit可以精確地控制查詢結(jié)果的數(shù)量,比如每次只查詢10條數(shù)據(jù)

格式

select * from 表名 limit 數(shù)值1, 數(shù)值2 ;

示例

select * from t_student limit 4, 8 ;

可以理解為:跳過最前面4條語句,然后取8條記錄

limit常用來做分頁查詢,比如每頁固定顯示5條數(shù)據(jù),那么應(yīng)該這樣取數(shù)據(jù)

第1頁:limit 0, 5

第2頁:limit 5, 5

第3頁:limit 10, 5

第n頁:limit 5*(n-1), 5

 

select * from t_student limit 7 ;這條語句的作用相當(dāng)于select * from t_student limit 0, 7 ;表示取最前面的7條記錄

 

四、約束

1.簡單約束

建表時可以給特定的字段設(shè)置一些約束條件,常見的約束有

not null :規(guī)定字段的值不能為null

unique :規(guī)定字段的值必須唯一

default :指定字段的默認(rèn)值

(建議:盡量給字段設(shè)定嚴(yán)格的約束,以保證數(shù)據(jù)的規(guī)范性)

 

示例

create table t_student (id integer, name text not null unique, age integer not null default 1) ;

name字段不能為null,并且唯一

age字段不能為null,并且默認(rèn)為1

 

2.主鍵約束

(1)簡單說明

如果t_student表中就name和age兩個字段,而且有些記錄的name和age字段的值都一樣時,那么就沒法區(qū)分這些數(shù)據(jù),造成數(shù)據(jù)庫的記錄不唯一,這樣就不方便管理數(shù)據(jù)

良好的數(shù)據(jù)庫編程規(guī)范應(yīng)該要保證每條記錄的唯一性,為此,增加了主鍵約束

也就是說,每張表都必須有一個主鍵,用來標(biāo)識記錄的唯一性

(2)什么是主鍵?

主鍵(Primary Key,簡稱PK)用來唯一地標(biāo)識某一條記錄

例如t_student可以增加一個id字段作為主鍵,相當(dāng)于人的身份證

主鍵可以是一個字段或多個字段

(3)主鍵的設(shè)計原則

主鍵應(yīng)當(dāng)是對用戶沒有意義的

永遠(yuǎn)也不要更新主鍵

主鍵不應(yīng)包含動態(tài)變化的數(shù)據(jù)

主鍵應(yīng)當(dāng)由計算機(jī)自動生成

(4)主鍵的聲明

在創(chuàng)表的時候用primary key聲明一個主鍵

create table t_student (id integer primary key, name text, age integer) ;

integer類型的id作為t_student表的主鍵

 

主鍵字段

只要聲明為primary key,就說明是一個主鍵字段

主鍵字段默認(rèn)就包含了not null 和 unique 兩個約束

 

說明:如果想要讓主鍵自動增長(必須是integer類型),應(yīng)該增加autoincrement

create table t_student (id integer primary key autoincrement, name text, age integer) ;

 

3.外鍵約束

利用外鍵約束可以用來建立表與表之間的聯(lián)系

外鍵的一般情況是:一張表的某個字段,引用著另一張表的主鍵字段

新建一個外鍵

create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_student_class foreign key (class_id) references t_class (id));

t_student表中有一個叫做fk_t_student_class_id_t_class_id的外鍵

這個外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段

 

4.表連接查詢

表連接查詢:需要聯(lián)合多張表才能查到想要的數(shù)據(jù)

表連接的類型

內(nèi)連接:inner join 或者 join  (顯示的是左右表都有完整字段值的記錄)

左外連接:left outer join (保證左表數(shù)據(jù)的完整性)

示例

查詢0316iOS班的所有學(xué)生

select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘0316iOS';

相關(guān)文章

最新評論