其中pon_con_db函数为连接数据库查询操作
pon_insert_db函数为连接数据库插入操作
#!/usr/bin/env python
-- coding:utf-8 --
import psycopg2
class pon_database():
def pon_con_db(self, sql): conn = psycopg2.connect(database="*************", user="***************", password="**************", host="****.****.****.*****",port="1234") try: cursor = conn.cursor() cursor.execute(sql) rs = cursor.fetchall() conn.commit() cursor.close() conn.close() return rs except: rs='' conn.close() return rs def pon_inser_db(self, sql): conn = psycopg2.connect(database="**********", user="***********", password="*************", host="****.****.****.*****",port="1234") try: cursor = conn.cursor() cursor.execute(sql) conn.commit() cursor.close() conn.close() # pass except: conn.close()
文章来源: https://blog.csdn.net/weixin_42384770/article/details/90266842