Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy:execute()获取了一个意外的关键字参数_Python_Sql_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy:execute()获取了一个意外的关键字参数

Python SQLAlchemy:execute()获取了一个意外的关键字参数,python,sql,sqlalchemy,Python,Sql,Sqlalchemy,我有database.py,其中包含数据库exec函数 import sqlachemy class DB(): def __init__(self, **kwargs): self.connection = None self.connect(**kwargs) def connect(self, **kwargs): if 'url' in kwargs and kwargs.get('url') is not None: return self.

我有
database.py
,其中包含数据库exec函数

import sqlachemy

class DB():
def __init__(self, **kwargs):
    self.connection = None
    self.connect(**kwargs)

def connect(self, **kwargs):
    if 'url' in kwargs and kwargs.get('url') is not None:
        return self.connectUrl(kwargs.get('url'), kwargs.get('username'), 
            kwargs.get('password'), kwargs.get('database'))
    else : 
        return self.connectHost(kwargs.get('host'), kwargs.get('port'), kwargs.get('username'), 
            kwargs.get('password'), kwargs.get('database'))

def execute(self, query):
    if self.connect is None:
        raise Exception('No connection')

    try:
        with self.connection.connect() as conn:
            return conn.execute(query)
    except (pymysql.err.OperationalError, sqlalchemy.exc.OperationalError) as e:
        print(e)
    except (Exception) as e:
        print(e)
    else:
        return None
我还有一个
main.py
。我从上面的文件导入DB类并在这里使用它

from database import db
# db connection
def connect():
   global db
   db = DB(
    database="DATABASE_NAME",
    username="DATABASE_USER",
    password="XXXXXXXXX",
    url="XXXXXXX",
    host="XXXXXXXXXXX",
    port="XXXXXXX",
    )

SQL_QUERY = """ select * from sample_table where country = :country and age = :age """

_age = 5
_country = 'US'
 
connect()
query = text(SQL_QUERY).bindparams(bindparam("country", String), bindparam("age", String))
db.execute(query, age=_age, country=_country)
当我试图执行这个脚本时,我得到了一个错误

db.execute(query,age=_age, country=_country) TypeError: execute() got an unexpected keyword argument 'age'

有人能帮我吗?

您可以简单地将变量作为元组传递:

query = 'select * from sample_table where country = ? and age = ?'
_age = 5
_country = 'US'
 
connect()

db.execute(query, (_age,_country,))

为什么“SQL=…”没有出现错误。您不能在中使用这样的sqlpython@eshirvana这是一个错误。我已经编辑了这个问题。请看一看。