因为我对不同的表有多个查询,使用不同的占位符,我想为此创建一个函数,但是我不能决定这个函数的参数
例如
def function_q(query, placeholder):
cursor.execute(query, (placeholder,))
return cursor.fetchall()
但是如果我需要调用这个函数时没有占位符,或者有一个以上的占位符,第二个参数应该是list
还是*args
?如果两个都是,我会得到错误。函数调用将是这样的,并且它是这样工作的:
person_id = 100
result = function_q('SELECT name FROM persons WHERE n_id = ?;', person_id)
但是如果我有不止一个占位符,这意味着函数调用将像这样:
person_id = 100
age = 30
result = function_q('SELECT name FROM persons WHERE n_id = ? and age = ?;', person_id, person_age)
1条答案
按热度按时间mzaanser1#
从
placeholder
变量cursor.execute(query, (placeholder,))
中删除元组,并假设它是一个可迭代对象。