标签:pass password post one turn 操作 col except exec
1 import psycopg2 as pg2 2 3 # 返回数据库PostgreSQL连接 4 def get_db_conn(): 5 # 创建连接 6 return pg2.connect(database=‘test‘, user=‘admin‘, password=‘admin‘, host=‘127.0.0.1‘, port=‘10001‘) 8 9 10 # 操作数据库PostgreSQL,返回一条结果 11 def db_fetchone(sql): 12 try: 13 conn = get_db_conn() 14 cur = conn.cursor() 15 cur.execute(sql) 16 rows = cur.fetchone() # 返回一条结果,返回多条结果使用rows = cur.fetchall() 17 18 return rows[0] 19 except pg2.DatabaseError as e: 20 print(‘Error $s‘ % e) 21 22 finally: 23 conn.commit() 24 cur.close() 25 conn.close()
标签:pass password post one turn 操作 col except exec
原文地址:https://www.cnblogs.com/yangtze-yufei/p/9488565.html