Tommy Chan
Tommy Chan

Reputation: 79

python return function(sql statement error)

I can return commands in my return function but something need to improve: 1. I am trying to get "enable" and "conf t"command in "function" field everytime.

B4(select function and type)(I can return my commands):

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:(select enable, conf t, and "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])

Hope you know what I mean but now I cant do what I want. It presents:

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

Upvotes: 0

Views: 43

Answers (1)

bheklilr
bheklilr

Reputation: 54058

The problem is that the .execute function expects the args passed in to be an iterable, even if there's only one item. When you have

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

This is equivalent to

cur.execute('QUERY', x)

So the execute method tries to iterate over x to get its arguments. Instead, pass in x inside a collection:

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

Upvotes: 1

Related Questions