Python SQLite的使用經驗

2022-02-12 03:17:43 字數 2213 閱讀 6611

sqlite是 一款輕量級的資料庫,很適合用著移動裝置上,或者是客戶端程式。sqlite的優點有:1. 不需要為資料庫起乙個單獨的程序 2. 整個資料庫可以隨時拷貝走 3. 不需要任何配置。從python 2.5開始,sqlite就在標準庫了,所以用起來比較方便。下面是我使用過程中的一些使用經驗。

import sqlite3 conn = sqlite3.connect

('/tmp/sqlite_db'

) cur = conn.cursor

()

create_table_stmt = ''

' create table if not exists test_table ( id text, duration integer, event_date text, parameter text );'

'' create_index = 'create index if not exists idx_id on test_table (id);' cur.execute

(create_table_stmt) cur.execute

(create_index) conn.commit

()

然後往裡面插一點資料吧,sqlite只支援5種基本的資料型別

null. the value is a null value.

integer. the value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

real. the value is a floating point value, stored as an 8-byte ieee floating point number.

text. the value is a text string, stored using the database encoding (utf-8, utf-16be or utf-16le).

blob. the value is a blob of data, stored exactly as it was input.

問題來了,sqlite的時間和日期型別在**?原來sqlite可以把時間日期儲存在一下幾種資料型別裡面

textas iso8601 strings (「yyyy-mm-dd hh:mm:ss.sss」).

realas julian day numbers, the number of days since noon in greenwich on november 24, 4714 b.c. according to the proleptic gregorian calendar.

integeras unix time, the number of seconds since 1970-01-01 00:00:00 utc.

insert_stmt = 'insert into test_table values (?, ?, ?, ?)' record = (

'test', 123, '2011-11-30 12:34:56', 'hello'

) cur.execute

(insert_stmt, record) conn.commit

()

例如找前一天存進去的資料:

select      id,      duration,      event_date,      parameter  from test_table where

date

(event_date)

=date

('now'

,'-1 day'

,'localtime'

)order

by id, event_date

select name from sqlite_master where

type

="table"

python SQLite學習筆記01 游標

這段對游標說的最清楚 使用cursor物件執行select語句時,通過featchall 返回select的全部資料結果集。結果集是乙個list,每個元素都是乙個tuple,對應一行記錄,按建表的字段順序排列。fetchone 返回一條結果,是乙個tuple,每個元素是乙個字段值。需要注意的是,sq...

python SQLite資料庫操作

sqlite是乙個軟體庫,實現了自給自足的 無伺服器的 零配置的 事務性的 sql 資料庫引擎。sqlite是乙個增長最快的資料庫引擎,這是在普及方面的增長,與它的尺寸大小無關。sqlite 源 不受版權限制。它是乙個零配置的資料庫,這意味著與其他資料庫一樣,您不需要在系統中配置。不需要乙個單獨的伺...

python sqlite3學習筆記

self.connect sqlite3.connect db name,timeout 3,isolation level none,check same thread false 引數1 db name 資料庫名稱 引數2 timeout 3 指當乙個資料庫被多個連線訪問,且其中乙個修改了資料庫...