python数据池连接PG

发现网上都是mysql,后面发现PG跟mysql差不多,记录下来,怕忘了

import psycopg2
from DBUtils.PooledDB import PooledDB
import psycopg2.extras

pool = PooledDB(psycopg2, 10,database="boatdb", user="postgres",
                                 password="xxxx", host="127.0.0.1", port="5432")
print("connect success")

conn = pool.connection()
cur = conn.cursor()
SQL = "select * from car_data"
r = cur.execute(SQL)
r = cur.fetchall()
cur.close()
conn.close()




下面是MySQL的

import MySQLdb
from DBUtils.PooledDB import PooledDB
pool = PooledDB(MySQLdb,5,host='localhost',user='root',passwd='pwd',db='myDB',port=3306) #5为连接池里的最少连接数

 
conn = pool.connection()  #以后每次需要数据库连接就是用connection()函数获取连接就好了
cur=conn.cursor()
SQL="select * from table1"
r=cur.execute(SQL)
r=cur.fetchall()
cur.close()
conn.close()

复制代码

你可能感兴趣的:(python数据池连接PG)