一些SQLite技巧

2021-09-22 23:26:05 字數 1875 閱讀 2907

sqlite自增id自段

使用方法為integer primary key autoincrement

如:create

table 21andy ( id 

integer

primary

key autoincrement, 21andy 

varchar(

100) 

notnull, date date );

注意是autoincrement, 和mysql的寫法不一樣

sqlite清空表

sqlite沒有truncate清空表命令,所以只能這樣

delete

from 21andy;

sqlite 刪除記錄後, 自增id置0

只能這樣

delete

from sqlite_sequence;

另外, 這個sqlite_sequence可以crud

sqlite 刪除記錄後, 不會釋放空間

必須像這樣

vacuum

sqlite 分頁查詢

有兩種寫法:

select

*from account limit 

9 offset 

10;select

*from account limit 

10, 

9 他們兩個的效果都是一樣的,其中第一種寫法比較清晰明了,即跳過10行,讀取其後的9行資料.

sqlite批量插入資料

很不幸的事情是貌似sqlite只能一條一條的執行插入,但是這是非常非常慢的行為,執行一條就是執行一次寫入磁碟的操作,這實在是太可怕了.在sqlite裡面執行批量插入,只能將插入操作放入到事務當中去.示例如下:

begin;

create

table t2(a 

integer, b 

integer, c 

varchar(

100));

insert

into t2 

values(

1,59672,

'fifty nine thousand six hundred seventy two

');

insert

into t2 

values(

24999,

89569,

'eighty nine thousand five hundred sixty nine

');

insert

into t2 

values(

25000,

94666,

'ninety four thousand six hundred sixty six

');

commit; 

在sqlite當中一系列要進行多次寫入操作的時候,建議放入到事務當中去,這個優化的效能提公升是可以很明顯感覺到的.用與不用的差別是非常大的.

sqlite資料不存在insert,存在update

sqlite的sql語法類mysql,在sqlite裡面有乙個關鍵字replace,可以使用它達到目的:

into

[table

] (row1, row2) 

values (

2, 3);

如果該錶有乙個主鍵,那麼當主鍵值相等的時候,該行資料不存在執行插入,存在則執行更新操作.但是如果沒有主鍵,那麼它就執行的永遠都是插入操作了.

一些SQLite技巧

sqlite自增id自段 使用方法為integer primary key autoincrement 如 create table 21andy id integer primary key autoincrement,21andy varchar 100 not null,date date 注...

SQLite一些函式用法

格林威治日期時間,比北京時間晚8小時 select datetime now 格林威治日期 select date now 本地時間 select time now localtime 日期時間格式化 select strftime y m d h m s now localtime 加1小時 se...

oracle SQL一些技巧

1 當前時間加7天 select sysdate interval 7 day from dual 2 當前時間減10分鐘 select sysdate interval 10 minute from dual 3 當前時間加3秒 select sysdate interval 3 second f...