自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊 Python 數(shù)據(jù)處理全家桶(PgSQL篇)

開發(fā) 后端
PgSQL,全稱為 PostgreSQL,是一款免費開源的關系型數(shù)據(jù)庫。相比最流行的 Mysql 數(shù)據(jù)庫,PgSQL 在可靠性、數(shù)據(jù)完整性、擴展性方面具有絕對的優(yōu)勢

 [[416887]]

本文轉(zhuǎn)載自微信公眾號「AirPython」,作者星安果。轉(zhuǎn)載本文請聯(lián)系AirPython公眾號。

1. 前言

PgSQL,全稱為 PostgreSQL,是一款免費開源的關系型數(shù)據(jù)庫

相比最流行的 Mysql 數(shù)據(jù)庫,PgSQL 在可靠性、數(shù)據(jù)完整性、擴展性方面具有絕對的優(yōu)勢

本篇文章將聊聊如何使用 Python 操作 PgSQL 數(shù)據(jù)庫

2. PgSQL 使用

Python 操作 PgSQL,需要先安裝依賴包「 psycopg2 」

  1. # 安裝依賴包 
  2. pip3 install psycopg2 

接下來,就可以使用 Python 來操作數(shù)據(jù)庫了

2-1 數(shù)據(jù)庫連接及游標對象

使用 psycopg2 中的「 connect() 」方法連接數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫連接對象及游標對象

  1. import psycopg2 
  2.  
  3. # 獲得連接對象 
  4. database:數(shù)據(jù)庫名稱 
  5. user:用戶名 
  6. password:密碼 
  7. # host:數(shù)據(jù)庫ip地址 
  8. # port:端口號,默認為5432 
  9. conn = psycopg2.connect(database="db_name"user="postgres"password="pwd", host="127.0.0.1", port="5432"
  10.  
  11. # 獲取游標對象 
  12. cursor = conn.cursor() 

獲取游標對象后,就可以執(zhí)行 SQL,進而操作數(shù)據(jù)庫了

2-2 插入數(shù)據(jù)

首先,編寫插入數(shù)據(jù)的 SQL 語句及參數(shù)( 可選 )

  1. # 構建SQL語句 
  2. # 方式一:直帶參數(shù) 
  3.  sql = "INSERT INTO student (name,age) \ 
  4.                         VALUES (%s, '%s')" % \ 
  5.           ('xag',23) 
  6.  
  7. # 方式二:參數(shù)分離 
  8. sql = """INSERT INTO student (name,age) VALUES (%s, %s)""" 
  9. # 參數(shù) 
  10. params = ('xag',23) 

然后,使用游標對象執(zhí)行 SQL

  1. # 執(zhí)行sql 
  2. # 注意:params可選,根據(jù)上面的參數(shù)方式來選擇設置 
  3. cursor.execute(sql,[params]) 

接著,使用連接對象提交事務

  1. # 事務提交 
  2. onn.commit() 

最后,釋放游標對象及數(shù)據(jù)庫連接對象

  1. # 釋放游標對象及數(shù)據(jù)庫連接對象 
  2. cursor.close() 
  3. conn.close() 

2-3 查詢數(shù)據(jù)

游標對象的 fetchone()、fetchmany(size)、fetchall() 這 3個函數(shù)即可以實現(xiàn)單條數(shù)據(jù)查詢、多條數(shù)據(jù)查詢、全部數(shù)據(jù)查詢

  1. # 獲取一條記錄 
  2. one_data = cursor.fetchone() 
  3. print(one_data) 
  4.  
  5. # 獲取2條記錄 
  6. many_data = cursor.fetchmany(2) 
  7. print(many_data) 
  8.  
  9. # 獲取全部數(shù)據(jù) 
  10. all_data = cursor.fetchall() 
  11. print(all_data) 

需要注意的是,條件查詢與上面的插入操作類似,條件語句可以將參數(shù)分離出來

  1. # 條件查詢 SQL語句 
  2. sql = """SELECT * FROM student where id = %s;""" 
  3.  
  4. # 對應參數(shù),參數(shù)結尾以逗號結尾 
  5. params = (1,) 
  6.  
  7. # 執(zhí)行SQL 
  8. cursor.execute(sql, params) 
  9.  
  10. # 獲取所有數(shù)據(jù) 
  11. datas = cursor.fetchall() 
  12. print(datas) 

2-4 更新數(shù)據(jù)

更新操作和上面操作一樣,唯一不同的是,執(zhí)行完 SQL 后,需要使用連接對象提交事務,才能將數(shù)據(jù)真實更新到數(shù)據(jù)庫中

  1. def update_one(conn, cursor): 
  2.     """更新操作""" 
  3.     # 更新語句 
  4.     sql = """update student set name = %s where id = %s  """ 
  5.     params = ('AirPython', 1,) 
  6.  
  7.     # 執(zhí)行語句 
  8.     cursor.execute(sql, params) 
  9.  
  10.     # 事務提交 
  11.     conn.commit() 
  12.  
  13.     # 關閉數(shù)據(jù)庫連接 
  14.     cursor.close() 
  15.     conn.close() 

2-5 刪除數(shù)據(jù)

刪除數(shù)據(jù)同更新數(shù)據(jù)操作類似

  1. def delete_one(conn, cursor): 
  2.     """刪除操作""" 
  3.     # 語句及參數(shù) 
  4.     sql = """delete from  student where id = %s  """ 
  5.     params = (1,) 
  6.  
  7.     # 執(zhí)行語句 
  8.     cursor.execute(sql, params) 
  9.  
  10.     # 事物提交 
  11.     conn.commit() 
  12.  
  13.     # 關閉數(shù)據(jù)庫連接 
  14.     cursor.close() 
  15.     conn.close() 

3. 最后

通過上面操作,可以發(fā)現(xiàn) Python 操作 PgSQl 與 Mysql 類似,但是在原生 SQL 編寫上兩者還是有很多差異性

更多關于 PgSQL 的操作可以參考下面鏈接:

https://www.postgresql.org/docs/14/index.html

 

責任編輯:武曉燕 來源: AirPython
相關推薦

2020-09-27 08:36:21

Python Memcached緩存

2020-09-29 08:35:08

MongoDBPython數(shù)據(jù)

2020-09-24 06:49:34

PythonRedis

2023-07-31 08:21:22

語法校對器Pick

2024-05-15 09:53:22

2025-04-17 08:00:00

前端UniApp數(shù)據(jù)存儲

2022-02-09 15:23:41

大數(shù)據(jù)流計算Spark

2022-07-15 08:45:07

slotVue3

2017-09-13 15:05:10

React前端單元測試

2017-09-10 17:41:39

React全家桶單元測試前端測試

2024-01-31 23:22:35

vaexPython

2021-03-16 10:12:24

python內(nèi)置函數(shù)

2023-12-12 11:06:37

PythonPandas數(shù)據(jù)

2023-09-25 13:19:41

pandasPython

2018-12-07 14:50:35

大數(shù)據(jù)數(shù)據(jù)采集數(shù)據(jù)庫

2020-11-02 15:56:04

大數(shù)據(jù)數(shù)據(jù)庫技術

2025-01-27 12:19:51

2017-07-21 14:22:17

大數(shù)據(jù)大數(shù)據(jù)平臺數(shù)據(jù)處理

2023-09-25 10:16:44

Python編程
點贊
收藏

51CTO技術棧公眾號