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

2024-04-24 10:17:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在返回函数中返回命令,但需要改进: 1.我每次都试图在“function”字段中获取“enable”和“conf t”命令

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])

After:(选择enable、conf t和“commands”)

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

Tags: orinfrom命令readexecuteenablefunction
1条回答
网友
1楼 · 发布于 2024-04-24 10:17:01

问题是.execute函数期望传入的参数是iterable,即使只有一个项。当你有

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

这相当于

cur.execute('QUERY', x)

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

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

相关问题 更多 >