Python操作PostgreSQL數(shù)據(jù)庫的基本方法(增刪改查)
前言
本文分享使用Python操作PostgreSQL數(shù)據(jù)庫的基本方法,包括數(shù)據(jù)庫連接、增、刪、改、查,供各位小伙伴參考。
一、連接PostgreSQL數(shù)據(jù)庫
操作MySQL數(shù)據(jù)庫主要使用psycopg2包,連接PostgreSQL數(shù)據(jù)庫的語法為connect(IP, 端口, 用戶名, 密碼, 數(shù)據(jù)庫名,編碼格式)。具體代碼如下:
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' )
二、增
(一)建表
以下語法中1、2、4、5為操作數(shù)據(jù)庫的通用語法,下文其他步驟也會使用。
- 創(chuàng)建mysql連接:pymysql.connect(),詳見上文第一點(diǎn)
- 創(chuàng)建游標(biāo):conn.cursor()
- 編寫建表語句,建表語句寫在三引號中:
(1)create_sql = “”“xxx”“”
(2)cursor.execute(create_sql) - 提交語句:conn.commit()
- 關(guān)閉mysql連接:conn.close()
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) cursor = conn.cursor() # 建表 create_sql = """ CREATE TABLE xxx.yyy( id int ,name varchar(10) ); """ cursor.execute(create_sql) print("create successfully") conn.commit() conn.close()
(二)插入數(shù)據(jù)
- 插入單條數(shù)據(jù)的語法和sql的插入類似,直接鍵值對插入,插入語句寫在雙引號中。具體代碼如下:
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) cursor = conn.cursor() # 插入 insert_sql = "INSERT INTO xx.yyy (id, name) VALUES (%s, '%s')" % (1, '張三') cursor.execute(insert_sql) print("insert successfully") conn.commit() conn.close()
- 查詢插入數(shù)據(jù),適用于從數(shù)據(jù)庫其他表查詢關(guān)聯(lián)后插入數(shù)據(jù)到新表。插入語句寫在三引號中。具體代碼如下:
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) cursor = conn.cursor() # 插入 insert_sql = """ DROP TABLE IF EXISTS xx.yyy; CREATE TABLE IF NOT EXISTS xx.yyy AS SELECT aa ,bb FROM xx.yyy """ cursor.execute(insert_sql) print("insert successfully") conn.commit() conn.close()
三、刪
(一)刪表
直接刪除表數(shù)據(jù)和表結(jié)構(gòu),其語法和SQL刪表語法一致。具體代碼如下:
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) cursor = conn.cursor() # 刪表 cursor.execute("DROP TABLE IF EXISTS xx.yyy") conn.commit() conn.close()
(二)刪除表數(shù)據(jù)
只刪除數(shù)據(jù),不刪除表結(jié)構(gòu)。其語法和SQL刪表數(shù)據(jù)語法一致。具體代碼如下:
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) cursor = conn.cursor() # 刪表數(shù)據(jù) cursor.execute("TRUCATE TABLE xx.yyy") conn.commit() conn.close()
三、改
(一)更新數(shù)據(jù)
將上文所建的yyy表中,“張三”改為“何老六”。具體代碼如下:
import psycopg2 conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) cursor = conn.cursor() cursor.execute("UPDATE xx.yyy SET name = '何老六' WHERE id = 1") conn.commit() conn.close()
(二)結(jié)果展示
四、查
(一)查詢數(shù)據(jù)庫表
直接使用pandas包查詢數(shù)據(jù)庫表,語法為read_sql(“select xxx from yyy”, con=數(shù)據(jù)庫連接)。
import psycopg2 import pandas as pd conn = psycopg2.connect( host='xxx', port='xxx', dbname='xxx', user='xxx', password='xxx' ) df = pd.read_sql("select * from xx.yyy limit 100;", con=conn) print(df)
總結(jié)
除了PostgreSQL數(shù)據(jù)庫外,業(yè)界常用的數(shù)據(jù)庫MySQL也可以使用Python進(jìn)行操作,具體情況可參考作者的另一篇博客Python操作MySQL數(shù)據(jù)庫。
到此這篇關(guān)于Python操作PostgreSQL數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Python操作PostgreSQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch中TensorBoard及torchsummary的使用詳解
這篇文章主要介紹了Pytorch中TensorBoard及torchsummary的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05Pycharm運(yùn)行時總是跳出Python?Console問題
這篇文章主要介紹了Pycharm運(yùn)行時總是跳出Python?Console問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04探索Python元類與class語句協(xié)議掌握類的控制權(quán)
這篇文章主要介紹了通過Python元類與class語句協(xié)議掌握類的控制權(quán)探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Django項(xiàng)目創(chuàng)建的圖文教程
本文主要介紹了Django項(xiàng)目創(chuàng)建的圖文教程,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python Yaml、Json、Dict之間的轉(zhuǎn)化
這篇文章主要介紹了python Yaml 、Json 、Dict 之間的轉(zhuǎn)化的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-10-10