SQLite基本文法

來源:互聯網
上載者:User

標籤:bool   des   test   分頁查詢   去除   phone   oca   命令列   求和   

命令列操作SQLite

建立一張表

create table 表名(欄位名字 欄位類型, 欄位名字 欄位類型);

create table info(id int,name varchar(20));

增:

       insert into 表名 values(要添加的值…);

       當添加的欄位的資料類型是int類型,直接寫值

       如果添加的欄位類型是varchar類型和日期類型,使用單引號把值包起來

       insert into info values(1,‘zhangsan‘,‘110‘);

        insert into test (name) values(‘lisi‘);

        插入記錄的時候可以在表名的後面加上(列名 ) 可以向指定的列中插入資料 

改:

   update 表名 set 欄位名1 = 值1 , 欄位名2 = 值2 where 條件;

    update info set phone=‘12345‘ where name=‘zhangsan‘;

刪:

      delete from 表名 where 條件 如果沒有where條件 刪除表中的所有資料

        delete from info where id > 1;

        delete from info; 刪除表中的所有資料

查:

     select 欄位名 (*) as別名  from 表名 where 條件

      select * from info; 查詢所有欄位的內容

      select name,phone from info where id = 1;

去除表中重複記錄(只會影響查詢的結果 不會修改表中的記錄)

       select distinct * from 表名;

         select distinct name from info; 只顯示名字 去除重複的記錄

 

部分查詢 : desc 降序 limit 查詢的個數,offset 座標

select number,mode from info order by _id desc  limit ? offset?  

讀取通訊錄連絡人,兩個表嵌套 

select location from Data1 where id=(select outkey from Data2 where num=1)

db.execSQL("create table info (_id integer primary key autoincreament,name varchar[20] ,phone varchar [20] )"  )

db.execSQL("alter table info add age integer " ) 插入某列

 

5使用sql對資料庫進行操作

建立資料庫

create database 資料庫名字;

顯示所有資料庫

show databases;

使用某個資料庫

use 資料庫的名字;

查看當前使用的資料庫是哪一個

select database();

刪除資料庫

drop database 資料庫名字;

 

6 使用sql對錶進行操作

建立一張表

create table 表名(欄位名字 欄位類型, 欄位名字 欄位類型);

create table info(id int,name varchar(20));

查看錶結構

desc 表名字;

desc info;

查看當前資料庫有哪些表

show tables;

刪除表

drop table 表名;

drop table info;

修改表結構 添加一列

alter table 表名 add 欄位名字 欄位類型;

alter table info add phone varchar(20);

 

sql支援的資料類型

字串型 

VARCHAR 可變長度的字串  varchar(20) 13888888888; 在申請的空間範圍內 用多少分配多少

CHAR  char(20);

大資料類型

BLOB 

TEXT 比較大的文本

數值型

TINYINT   byte

SMALLINT short

INT    int

BIGINT long

FLOAT float

DOUBLE double

邏輯性 

BIT  boolean

日期型

DATE 日期

TIME   時間

DATETIME 日期和時間

TIMESTAMP 時間戳記

 

 

7使用sql對錶中的記錄進行操作 CRUD   ☆☆☆☆☆ ☆☆☆☆☆

1.插入記錄

       insert into 表名 values(要添加的值…);

       當添加的欄位的資料類型是int類型,直接寫值

       如果添加的欄位類型是varchar類型和日期類型,使用單引號把值包起來

 

        insert into info values(1,‘zhangsan‘,‘110‘);

        insert into test (name) values(‘lisi‘);

        插入記錄的時候可以在表名的後面加上(列名 ) 可以向指定的列中插入資料 

2.修改記錄

       update 表名 set 欄位名1 = 值1 , 欄位名2 = 值2 where 條件;

    update info set phone=‘12345‘ where name=‘zhangsan‘;

3.刪除記錄

       delete from 表名 where 條件 如果沒有where條件 刪除表中的所有資料

        delete from info where id = 1;

        delete from info; 刪除表中的所有資料

4.查詢記錄

       select 欄位名 (*) as別名  from 表名 where 條件

      select * from info; 查詢所有欄位的內容

      select name,phone from info where id = 1;

5.去除表中重複記錄(只會影響查詢的結果 不會修改表中的記錄)

       select distinct * from 表名;

         select distinct name from info; 只顯示名字 去除重複的記錄

6.MySQL的約束

       a.非空約束  not  null

              * 表示資料不可為空

       b.唯一性限制式  unique

              * 表中的記錄不能重複的

       c.主鍵約束  primary key

              * 表示非空,唯一性

         d 自動成長 auto_increment

create table test(id int primary key auto_increment,name varchar(20));

 

8where條件的使用

.where子句使用

       a. 運算子 <, >,  >=,  <=

       b. in 在範圍內

select * from student where Android in (70,90);   注意in不是指定起始和結束的範圍 而是在()中的資料中做選擇

 

 如果想指定起始和結束的範圍要使用and 條件

c. and  條件同時滿足

 select * from student where Android>=70 and Android<=90;

 

       d. like 模糊查詢

        % 表示預留位置(萬用字元) %替換若干文字

      select * from student where name like ‘%zha%‘;

     //查詢表中名字裡含有zha的記錄

 

 

 

排序

       order by 欄位名 asc   升序

       order by 欄位名 desc 降序

         預設升序的 如果想降序 需要加上desc

select * from student order by English;

 

 

9彙總函式,分組查詢&limit關鍵字

1.count()函數

       select count(*) from ...where....

2.sum()函數

       select sum(要進行求和欄位) from ...where.... 

3. avg()函數

       select avg(要計算平均數的欄位名稱) from …

4. max()函數 

       select max(欄位) from...

5. min()函數 

       select min(欄位) from... 

分組查詢 group by

 

 

select name,sum(price) from orders group by name;

按照名字進行分組 查詢每種商品的總價格

 

 分組查詢如果加上條件不能使用where 而要使用having

 select name,sum(price) from orders group by name having sum(price)>5000;

 

limit

 select * from orders limit 2; 從第一條開始顯示 顯示2條

select * from orders limit 2,3; 從第2+1條開始顯示 顯示3條記錄;

通過limit關鍵字 可以實現資料的分頁查詢(只查詢一部分資料)

 

SQLite基本文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.