Python 操作 PostgreSQL 數(shù)據(jù)庫(kù)示例【連接、增刪改查等】
本文實(shí)例講述了Python 操作 PostgreSQL 數(shù)據(jù)庫(kù)。分享給大家供大家參考,具體如下:
我使用的是 Python 3.7.0
PostgreSQL可以使用psycopg2模塊與Python集成。
sycopg2是用于Python編程語(yǔ)言的PostgreSQL數(shù)據(jù)庫(kù)適配器。
psycopg2是非常小,快速,穩(wěn)定的。 您不需要單獨(dú)安裝此模塊,因?yàn)槟J(rèn)情況下它會(huì)隨著Python 2.5.x版本一起發(fā)布。
pip3 install python-psycopg2 pip3 install psycopg2-binary
連接到數(shù)據(jù)庫(kù)
以下Python代碼顯示了如何連接到現(xiàn)有的數(shù)據(jù)庫(kù)。 如果數(shù)據(jù)庫(kù)不存在,那么它將自動(dòng)創(chuàng)建,最后將返回一個(gè)數(shù)據(jù)庫(kù)對(duì)象。
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
在這里指定使用testdb作為數(shù)據(jù)庫(kù)名稱,如果數(shù)據(jù)庫(kù)已成功打開(kāi)連接,則會(huì)提供以下消息:
Open database successfully
創(chuàng)建表
以下Python程序?qū)⒂糜谠谙惹皠?chuàng)建的數(shù)據(jù)庫(kù)(testdb)中創(chuàng)建一個(gè)表:
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
cur.execute('''CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
print "Table created successfully"
conn.commit()
conn.close()
當(dāng)執(zhí)行上述程序時(shí),它將在數(shù)據(jù)庫(kù)testdb中創(chuàng)建COMPANY表,并顯示以下消息:
Opened database successfully
Table created successfully
插入操作
以下Python程序顯示了如何在上述示例中創(chuàng)建的COMPANY表中創(chuàng)建記錄:
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1, 'Paul', 32, 'California', 20000.00 )");
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");
conn.commit()
print("Records created successfully");
conn.close()
當(dāng)執(zhí)行上述程序時(shí),它將在COMPANY表中創(chuàng)建/插入給定的記錄,并顯示以下兩行:
Opened database successfully
Records created successfully
SELECT操作
以下 Python 程序顯示了如何從上述示例中創(chuàng)建的 COMPANY 表中獲取和顯示記錄:
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
cur.execute("SELECT id, name, address, salary from COMPANY")
rows = cur.fetchall()
for row in rows:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3], "\n")
print("Operation done successfully");
conn.close()
執(zhí)行上述程序時(shí),會(huì)產(chǎn)生以下結(jié)果:
Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0Operation done successfully
更新操作
以下 Python 代碼顯示了如何使用UPDATE語(yǔ)句來(lái)更新任何記錄,然后從COMPANY表中獲取并顯示更新的記錄:
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
conn.commit
print("Total number of rows updated :", cur.rowcount)
cur.execute("SELECT id, name, address, salary from COMPANY")
rows = cur.fetchall()
for row in rows:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3], "\n")
print("Operation done successfully");
conn.close()
Python
執(zhí)行上述程序時(shí),會(huì)產(chǎn)生以下結(jié)果:
Opened database successfully
Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000.0ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0Operation done successfully
刪除操作
以下 Python 代碼顯示了如何使用 DELETE 語(yǔ)句來(lái)刪除記錄,然后從 COMPANY 表中獲取并顯示剩余的記錄:
#!/usr/bin/python
import psycopg2
conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
cur.execute("DELETE from COMPANY where ID=2;")
conn.commit
print("Total number of rows deleted :", cur.rowcount)
cur.execute("SELECT id, name, address, salary from COMPANY")
rows = cur.fetchall()
for row in rows:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3], "\n")
print("Operation done successfully");
conn.close()
執(zhí)行上述程序時(shí),會(huì)產(chǎn)生以下結(jié)果:
Opened database successfully
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0Operation done successfully
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 使用python將mdb數(shù)據(jù)庫(kù)文件導(dǎo)入postgresql數(shù)據(jù)庫(kù)示例
- Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法
- Python實(shí)現(xiàn)連接postgresql數(shù)據(jù)庫(kù)的方法分析
- python連接PostgreSQL數(shù)據(jù)庫(kù)的過(guò)程詳解
- python 兩個(gè)數(shù)據(jù)庫(kù)postgresql對(duì)比
- Python操作PostgreSql數(shù)據(jù)庫(kù)的方法(基本的增刪改查)
- Python操作PostgreSQL數(shù)據(jù)庫(kù)的基本方法(增刪改查)
相關(guān)文章
Python的Flask框架開(kāi)發(fā)驗(yàn)證碼登錄的實(shí)現(xiàn)
在本文我們介紹了如何使用Python的Flask框架開(kāi)發(fā)一個(gè)簡(jiǎn)單的驗(yàn)證碼登錄功能,將涵蓋生成驗(yàn)證碼、處理用戶輸入、驗(yàn)證驗(yàn)證碼以及實(shí)現(xiàn)安全的用戶認(rèn)證等方面,感興趣的可以了解一下2023-11-11
Python Matplotlib繪制動(dòng)畫(huà)的代碼詳解
使用matplotlib可以很容易地創(chuàng)建動(dòng)畫(huà)框架。在本文中我們就將利用Matplotlib制作幾個(gè)簡(jiǎn)單的動(dòng)畫(huà),文中的示例代碼講講詳細(xì),感興趣的可以了解下2022-05-05
Python實(shí)現(xiàn)刪除排序數(shù)組中重復(fù)項(xiàng)的兩種方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)刪除排序數(shù)組中重復(fù)項(xiàng)的兩種方法,涉及Python數(shù)組元素的遍歷、判斷、刪除等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
Matplotlib實(shí)戰(zhàn)之折線圖繪制詳解
折線圖是一種用于可視化數(shù)據(jù)變化趨勢(shì)的圖表,它可以用于表示任何數(shù)值隨著時(shí)間或類別的變化,本文主要介紹了如何利用Matplotlib實(shí)現(xiàn)折線圖的繪制,感興趣的可以了解下2023-08-08
pytest接口測(cè)試之fixture傳參數(shù)request的使用
本文主要介紹了pytest接口測(cè)試之fixture傳參數(shù)request的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
python json.dumps中文亂碼問(wèn)題解決
這篇文章主要介紹了python json.dumps中文亂碼問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python中的反射知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于Python中的反射知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-11-11

