新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python操作PgSQL數(shù)據(jù)庫的方法
1、Python操作PgSQL,需要先安裝依賴包psycopg2。

# 安裝依賴包 pip3 install psycopg2
2、使用psycopg2中的connect()連接數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫連接對象和游標(biāo)對象。
獲得游標(biāo)對象后,可以執(zhí)行SQL,然后操作數(shù)據(jù)庫。
import psycopg2 # 獲得連接對象 # database:數(shù)據(jù)庫名稱 # user:用戶名 # password:密碼 # host:數(shù)據(jù)庫ip地址 # port:端口號,默認(rèn)為5432 conn = psycopg2.connect(database="db_name", user="postgres", password="pwd", host="127.0.0.1", port="5432") # 獲取游標(biāo)對象 cursor = conn.cursor()
以上就是Python操作PgSQL數(shù)據(jù)庫的方法,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:Python操作PgSQL數(shù)據(jù)庫的方法
文章URL:http://fisionsoft.com.cn/article/dhpjhji.html


咨詢
建站咨詢
