1. 程式人生 > 其它 >SQLite基本指令

SQLite基本指令

技術標籤:資料庫sqlitesqlite3指令

–這是一個單行註釋
/*
這是一個多行註釋
*/
在這裡插入圖片描述

表操作

建立表

–注意:SQL語句中關鍵字不區分大小寫

--語法格式
    Create Table    表名稱(
        欄位1 型別 約束,
        欄位2 型別 約束,
        欄位3 型別 約束,
        .............
    );
  /*
    欄位:列,  列名稱
    型別:  該欄位的資料型別
            integer    整型
            Real       實型(小數)
            text       文字
            blob       二進位制
    約束:   對該欄位起約束性,可有可無
    
    注意:資料庫中不能有重名錶
    */
--例: --建立學生資訊表,包括 學號,姓名,年齡,成績,性別 Create Table Student( num int, name text, age int, score real, sex text );

if not exists

沒有就建立,有就忽略 if not exists,位置:表名稱前

--建立英雄表,包括 英雄名稱 ,等級,攻擊力,職業,血量
--沒有就建立,有就忽略 if not exists,位置:表名稱前
Create Table if not exists Hero(
name text,
level int,
attck int,
career text
, bloob int );

刪除表

--格式 :Drop table 表名稱;
Drop table Student;

資料操作

新增/插入資料

/*
--格式: Insert into 表名稱(欄位1,欄位2,欄位3.............)
            values(值1,值2,值3,..........);
      功能:向指定表中新增一條記錄
          欄位1,欄位2:記錄中被新增資料的欄位
      值1,值2 :對應欄位的資料
      
     注意:值的型別必須與欄位的型別一致
             所有的欄位必須是表中擁有的
     
     字串必須使用雙引號/單引號括起來
 */
--向學生表中新增一條記錄 Insert into Student(num,name,age,score,sex)values(10001,'小啊giao',11,13,'人妖'); //順序部分先後,但要一一對照 Insert into Student(score,num,name,age,sex)values(13,104341,'射會giao',11,'男');

刪除資料

--格式:delete from 表名; 

--例:
--刪除 學生表中所有資料
delete from Student;


--格式:delete from 表名 where 條件;

--例:
--刪除 學生表中性別為人妖的資料
delete from Student where sex='人妖';

修改資料

--格式 update 表名 set 要更改的資料;

--例:
--更改學生表中 所有人 分數為100分
update Student set score =100;


--格式 update 表名 set 要更改的資料 where 條件;
--例:
--更改學生表中 性別為男  年齡修成為 250,簡單的說就是 修改年齡為250  條件是 性別為男
update Student set age=250 where sex='男';

查詢資料

* 如果要顯示錶中所有 可以用 星號代替

/*
格式:
    select 欄位1,欄位2,欄位3 ....
    from 表名稱
    [where 條件];
    
    欄位1,欄位2 : 記錄中 需要被顯示的欄位
                如果顯示每條記錄中所有欄位,則可以用*代替
    表名稱:對應查詢的表
    [] : 可有可無
        無:代表顯示整張表中的所有記錄
        有:顯示滿足條件的記錄
*/
--顯示學生表中所有的資料

select *from Student;

--顯示性別為 人妖 的所有記錄
select * from Student Where sex='人妖';

--顯示學生表中年齡等於250 的學生名字 成績
select name,score from Person where age=250;

邏輯運算子 與比較

比較 > < =
邏輯運算 And Or