SQLite經常使用SQL語句

SQLite經常使用SQL語句

建立表格

sql="CREATE TABLE IF NOT EXISTS MusicList (id integer primary key AutoIncrement,name varchar(20),path varchar(20))";

查詢表格

查看錶結構
desc <table>; sql

查看全部數據
select * from <table>; dom

查看指定的列數據
select , from <table>; 函數

查看非重複數據
select distinct , from <table>; spa

複製數據
insert into users(_id,username,password) select * from users; code

首字母爲S的數據
select username from users where username like 'S%' ; sqlite

第三個字母爲S的數據
select username from users where username like '__S%' ; rem

查詢001,220,230的數據
select * from users where _id in(001,220,230); 字符串

順序查詢
select * from users where _id in(001,220,230); it

反順序查詢
select * from user order by _id desc; io

分頁功能

獲取數據行總數
select count(word) as number from <table>;

分頁查詢
select , from <table> order by word limit 100 offset 200;

SQLite內建函數表

算術函數

  • abs(X) 返回給定數字表達式的絕對值。
  • max(X,Y[,...]) 返回表達式的最大值。
  • min(X,Y[,...]) 返回表達式的最小值。
  • random(*)返回隨機數。
  • round(X[,Y])返回數字表達式並四捨五入爲指定的長度或精度。

字符處理函數

  • length(X) 返回給定字符串表達式的字符個數。
  • lower(X)將大寫字符數據轉換爲小寫字符數據後返回字符表達式。
  • upper(X)返回將小寫字符數據轉換爲大寫的字符表達式。
  • substr(X,Y,Z)返回表達式的一部分。
  • randstr()
  • quote(A)
  • like(A,B)肯定給定的字符串是否與指定的模式匹配。
  • glob(A,B)

條件判斷函數

  • coalesce(X,Y[,...])
  • ifnull(X,Y)
  • nullif(X,Y)

集合函數

  • avg(X)返回組中值的平均值。
  • count(X)返回組中項目的數量。
  • max(X)返回組中值的最大值。
  • min(X)返回組中值的最小值。
  • sum(X)返回表達式中全部值的和。

其餘函數

  • typeof(X)返回數據的類型。
  • last_insert_rowid()返回最後插入的數據的ID。
  • sqlite_version(*)返回SQLite的版本。
  • change_count()返回受上一語句影響的行數。
  • last_statement_change_count()
相關文章
相關標籤/搜索