Python 数据库操作 psycopg2
生活随笔
收集整理的這篇文章主要介紹了
Python 数据库操作 psycopg2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 安裝
- 基本使用
安裝
psycopg 是 Python 語言中 PostpreSQL數據庫接口
安裝環境:
- Python:v2.7, v3.4~3.8
- PostGreSQL:7.4~12
pip install psycopg2
基本使用
import psycopg2def connect_db(host: str,port: int,database: str,user: str,password: str,) -> tuple:conn = psycopg2.connect(host=host,port=port,database=database,user=user,password=password)return conndef execute_sql(conn: object, sql: str, params: tuple = ()) -> list:cur = conn.cursor()cur.execute(sql, params)try:return cur.fetchall()except psycopg2.ProgrammingError:conn.commit()return
總結
以上是生活随笔為你收集整理的Python 数据库操作 psycopg2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 常见操作指令
- 下一篇: Python multiprocess