
Python:3.8.2 Windows:10 pip:20.1 pip安装
pip安装 python连接数据库,需要使用到db连接工具包,这里使用的是:psycopg2
pip install psycopg2
安装完成,运行一下import,如果没有错误及模块加载成功

数据库操作示例代码
import psycopg2
#创建连接对象
conn=psycopg2.connect(database="postgres",user="postgres",password="postgres",host="localhost",port="5432")
#创建指针对象
cur=conn.cursor()
# 执行sql
cur.execute('select * from postgres.tbl_user')
results=cur.fetchall()
print ('查询结果',results)
cur.execute('update itmngr.tbl_user set login_name = \'xxx1\' where user_id = 1');
results = cur.rowcount
# commit 一下,不然修改不提交
conn.commit()
print ('影响行:',results)更多使用说明参见 python-db 关于python工具包安装 当没有网络的时候,下载whl文件,然后使用pip 安装
#注意替换文件名
pip install *.whl各种whl离线包下载地址:https://pypi.org 如果连pip都木有,请自行google pip的安装或者找寻对应的源码(.py)进行安装