python 內建 sqlite3 資料庫
python 內建 sqlite3 資料庫
資料庫處理步驟
(1)用connection()建立與資料庫檔案聯繫的Connection物件
(2)由Connection物件的course() 方法建立Cursor物件
(3)利用Cursor物件的execute()方法進行資料庫的操作
(4)利用Connection物件的commit()方法更新資料庫檔案
(5)利用Connection物件的close()方法關閉資料庫檔案聯繫
基礎SQL語法
CREATE TABLE tablename (field1 type,field2 type, . . .)
INSERT INTO tablename VALUES (value1,value2, . . .)
SELECT field FROM tablename
SELECT field FROM tablename WHERE condition
SELECT field FROM tablename ORDER BY field
SELECT field FROM name BETWEEN value1 AND Value2
SELECT AVG(field) FROM tablename
SELECT COUNT(field) FROM tablename
SELECT MAX(field) FROM tablename
SELECT MIN(field) FROM tablename
SELECT SUM(field) FROM tablename
DELETE FROM name
DELETE FROM name WHERE condition
sql type
Null
INTEGER
REAL
TEXT
BLOB
import sqlite3
conn = splite3.connect(‘example.db’)
c=conn.cursor()
c.execute(“CREAT TABLE contracts(date text,name text,number text)”)
c.execute(“INSERT INTO contacts VALUSE (‘date1’,’name1’,’number1’)”)
c.execute(“INSERT INTO contacts VALUSE (‘date2,’name2’,’number2’)”)
c.execute(“INSERT INTO contacts VALUSE (‘date2’,’name2’,’number2’)”)
for row in c.execute(‘SELECT * FROM contacts ORDER BY number’)
print(row)
conn.commit()
conn.close()