0

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
1
  • 1
    Use (x,) instead of (x). The former is a 1-length tuple, the latter is just x. Commented Mar 3, 2015 at 16:57

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.