python返回函数(sql语句错误)

时间:2015-03-03 16:55:33

标签: python mysql sqlite

我可以在return函数中返回命令,但需要改进: 我正试图让#34;启用"和" conf"命令"功能"每次都是场。

B4(选择功能和类型)(我可以返回我的命令):

def readrouter(x, y):
        conn = sqlite3.connect('server.db')
        cur = conn.cursor()
        cur.execute("SELECT DISTINCT command FROM router WHERE   function =? or type = ?  ORDER BY key ASC",(x, y))
        read = cur.fetchall()
        return read;

a = raw_input("x:")
b = raw_input("y:")
for result in readrouter(a,b):
    print (result[0])

之后:(选择启用,配置和"命令")

def readrouter(x):
        conn = sqlite3.connect('server.db')
        cur = conn.cursor()
        cur.execute("SELECT DISTINCT command FROM router WHERE   function =? or function='configure terminal' or function='enable'  ORDER BY key ASC",(x))
        read = cur.fetchall()
        return read;

a = raw_input("x:")
for result in readrouter(a):
    print (result[0])

希望你知道我的意思,但现在我无法做我想做的事。 它提出了:

x:create vlan
Traceback (most recent call last):
  File "C:/Users/f0449492/Desktop/2015225/database.py", line 323, in <module>
    for result in readrouter(a):
  File "C:/Users/f0449492/Desktop/2015225/database.py", line 318, in readrouter
    cur.execute("SELECT  command FROM router WHERE   function =? or function='configure terminal' or function='enable'  ORDER BY key ASC",(x))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied.

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

问题是.execute函数希望传入的args是可迭代的,即使只有一个项。当你有

cur.execute('QUERY', (x))

这相当于

cur.execute('QUERY', x)

因此execute方法尝试迭代x以获取其参数。相反,在集合中传递x

cur.execute('QUERY', (x,))
# OR
cur.execute('QUERY', [x])