在python中使用sqlalchemy时,'Connection'对象不可调用

时间:2016-06-09 05:24:02

标签: python-3.x sqlalchemy

我正在

  

连接'对象不可调用

执行我正在使用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是无

0 个答案:

没有答案