2

I'm having a trouble when trying to use function to query data in MSSQL using pyodbc, the function is:

def getUserList(connection):
    li = []
    cur = connection.cursor()
    query = 'select username, password, firstname, lastname, description, phone,'+
            'email, isAdmin, isAutoBoot from users_tbl'    
    cur.execute(query)
    for usr, pwd, fn, ln, des, ph, em, ad, au in cur.fetchall():        
        temp = User(usr, pwd, fn, ln, des, ph, em, ad, au)            
    cur.close()
    #con.close()
    return li

When I import the module and run this function, it encounter the TypeError:

Traceback (most recent call last):
File "<pyshell#71>", line 1, in <module>
  import_user.getUserList(s_con)
File "c:/python27/mymodule\import_user.py", line 12, in getUserList
  cur.execute(query)
TypeError: string indices must be integers

However if i copy and run each of these line in Python IDLE, it runs fine and no error when execute the cursor. It happens both when I pass pyodbc or sqlite connection as input parameter.

Thanks

1
  • 2
    I get SyntaxError on the line that ends with +. Commented May 18, 2012 at 5:56

1 Answer 1

1

To resolve syntax problem, change these lines:

query = 'select username, password, firstname, lastname, description, phone,'+
        'email, isAdmin, isAutoBoot from users_tbl'

to either (newline not preserved):

query = ('select username, password, firstname, lastname, description, phone, '
         'email, isAdmin, isAutoBoot from users_tbl')

or (newline preserved):

query = """select username, password, firstname, lastname, description, phone, 
           email, isAdmin, isAutoBoot from users_tbl"""

See the Maximum Line Length section of the style guide for more info on line wrapping.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks beargle but it does not resolve the problem, I even tried to put the whole query string in one line. Anyway, I restarted IDLE and run it again and the function works fine, must be something that's interfere with the module import. Apparently the function works fine if I run the module from command line

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.