1. 程式人生 > 資料庫 >Python SQLite3簡介

Python SQLite3簡介

最近需要用Python寫一個簡易通訊錄,但是對於資料儲存很發愁。大家都知道,使用 Python 中的列表和字典進行儲存資料是很不靠譜的,所以就想到Python有沒有內建的資料庫模組。

SQLite3簡介

SQLite3 可使用 sqlite3 模組與 Python 進行整合。sqlite3 模組是由 Gerhard Haring 編寫的。它提供了一個與 PEP 249 描述的 DB-API 2.0 規範相容的 SQL 介面。您不需要單獨安裝該模組,因為 Python 2.5.x 以上版本預設自帶了該模組。
為了使用 sqlite3 模組,您首先必須建立一個表示資料庫的連線物件,然後您可以有選擇地建立游標物件,這將幫助您執行所有的 SQL 語句。

怎麼樣,聽起來不錯吧!那就快來學習使用吧。

連線資料庫

下面的 Python 程式碼顯示瞭如何連線到一個現有的資料庫。如果資料庫不存在,那麼它就會被建立,最後將返回一個數據庫物件。

#-*- coding:utf-8 -*-
import sqlite3
conn = sqlite3.connect('mysql_person.db')

print "Opened database successfully";

在這裡,您也可以把資料庫名稱複製為特定的名稱 :memory:,這樣就會在 RAM 中建立一個數據庫。現在,讓我們來執行上面的程式,在當前目錄中建立我們的資料庫 mysql_person.db。您可以根據需要改變路徑。儲存上面程式碼到 sqlite.py 檔案中,並按如下所示執行。如果資料庫成功建立,那麼會顯示下面所示的訊息:

$chmod +x sqlite.py
$./sqlite.py
Open database successfully

建立表

下面的 Python 程式碼段將用於在先前建立的資料庫中建立一個表:

#-*- coding:utf-8 -*-

import sqlite3

conn = sqlite3.connect('mysql_person.db')
print "Opened database successfully";

conn.execute('''CREATE TABLE MT
    (ID INT PRIMARY KEY   NOT NULL,NAME      TEXT  NOT NULL,AGE      INT   NOT NULL,ADDRESS    CHAR(50),SALARY     REAL);''')
print "Table created successfully";

conn.close()

上述程式執行時,它會在 test.db 中建立 MT 表,並顯示下面所示的訊息:

Opened database successfully
Table created successfully

INSERT 操作

下面的 Python 程式顯示瞭如何在上面建立的 MT 表中建立記錄:

#-*- coding:utf-8 -*-

import sqlite3

conn = sqlite3.connect('mysql_person.db')
print "Opened database successfully";

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
   VALUES (1,'Paul',32,'California',20000.00 )");

conn.execute("INSERT INTO COMPANY (ID,SALARY) \
   VALUES (2,'Allen',25,'Texas',15000.00 )");

conn.execute("INSERT INTO COMPANY (ID,SALARY) \
   VALUES (3,'Teddy',23,'Norway',SALARY) \
   VALUES (4,'Mark','Rich-Mond ',65000.00 )");

conn.commit()
print "Records created successfully";
conn.close()

上述程式執行時,它會在 MT 表中建立給定記錄,並會顯示以下兩行:

Opened database successfully
Records created successfully

SELECT 操作

下面的 Python 程式顯示瞭如何從前面建立的 MT 表中獲取並顯示記錄:

#-*- coding:utf-8 -*-

import sqlite3

conn = sqlite3.connect('mysql_person.db')
print "Opened database successfully";

cursor = conn.execute("SELECT id,name,address,salary from MT")
for row in cursor:
  print "ID = ",row[0]
  print "NAME = ",row[1]
  print "ADDRESS = ",row[2]
  print "SALARY = ",row[3],"\n"

print "Operation done successfully";
conn.close()

上述程式執行時,它會產生以下結果:

Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0

ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

UPDATE 操作

下面的 Python 程式碼顯示瞭如何使用 UPDATE 語句來更新任何記錄,然後從 COMPANY 表中獲取並顯示更新的記錄:

#-*- coding:utf-8 -*-

import sqlite3

conn = sqlite3.connect('mysql_person.db')
print "Opened database successfully";

conn.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
conn.commit()
print "Total number of rows updated :",conn.total_changes

cursor = conn.execute("SELECT id,"\n"

print "Operation done successfully";
conn.close()

上述程式執行時,它會產生以下結果:

Opened database successfully
Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000.0

ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

DELETE 操作

下面的 Python 程式碼顯示瞭如何使用 DELETE 語句刪除任何記錄,然後從 COMPANY 表中獲取並顯示剩餘的記錄:

#-*- coding:utf-8 -*-

import sqlite3

conn = sqlite3.connect('mysql_person.db')
print "Opened database successfully";

conn.execute("DELETE from COMPANY where ID=2;")
conn.commit()
print "Total number of rows deleted :",salary from MT")
for row in cursor:
 print "ID = ",row[0]
 print "NAME = ",row[1]
 print "ADDRESS = ",row[2]
 print "SALARY = ","\n"

print "Operation done successfully";
conn.close()

上述程式執行時,它會產生以下結果:

Opened database successfully
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。