我正在
执行我正在使用sqlalchemy包的python代码时出现连接'对象不可调用
错误。
import sys
import pymysql
import sqlalchemy.pool as pool
from init import db_config
DEBUG="True"
def _get_connection(**kwargs):
try:
return pymysql.Connection(**kwargs);
except pymysql.OperationalError as e:
if DEBUG=="True": print("Exception catched %s" %e.message)
return None
def connect_db(kwargs=db_config):
try:
conn = _get_connection(**kwargs)
if not conn == None:
mypool = pool.QueuePool(conn, max_overflow=1, pool_size=1)
print("====Type of pool", type(mypool))
local_conn = mypool.connect()
print("LocalConn object given: ", local_conn)
return local_conn
else:
print("conn is None")
except TypeError as e:
if DEBUG=="True": print("Exception catched during queue pooling:", e)
return None
def disconnect_db(dbh):
dbh.close()
def fetch_rows(query):
query = str(query)
if not query:
if DEBUG=="True": print("Input query is empty")
return None
rows=[]
local_conn = connect_db();
if not local_conn == None:
try:
print("fetch_rows.local_conn:", local_conn);
with local_conn.cursor() as cursor:
cursor.execute(query);
rows = cursor.fetchall();
except pymysql.OperationalError as e:
if DEBUG=="True": print("FetchRows excep:", e)
finally:
disconnect_db(local_conn)
else:
print("fetch_rows.local_conn is None");
return rows
队列池中捕获异常:'Connection'对象不可调用 fetch_rows.local_conn是无