Pyodbc problem: code not working in IDE: AttributeError

This is the place for queries that don't fit in any of the other categories.

Pyodbc problem: code not working in IDE: AttributeError

Postby gunglichen » Tue Oct 08, 2013 3:05 am

Hello all,

Thank you for taking the time to read this cry for help.

I have just started learning Python (version 3.3) mainly to manipulate data from a SQL Server 2005 database as I find the SQL language a bit limiting.

I have cobbled together a few lines of code to return rows in a small SQl Server table using the pyodbc module. When run line by line in IDLE I get the results I expect, however when run as a program in my IDE (Aptana Studio) I get an AttributeError. With no experience with Python I'm not sure how to resolve this problem. I will greatly appreciate any help from you wisened Python coders out there.

The code is as follows:

Code: Select all

import pyodbc

def main():
    cnxn = pyodbc.connect('DSN=SQL2005;UID=xxx;PWD=xxx')
    cursor = cnxn.cursor()
    cursor.execute("select * from Users")
    row = cursor.fetchone()
    while row:
        print(row)
        row = cursor.fetchone()

if __name__ == "__main__": main()



The error the interpreter rudely throws back is as follows:

Traceback (most recent call last):
File "C:\Users\administrator\Desktop\Exercise Files\16 Databases\pyodbc.py", line 17, in <module>
if __name__ == "__main__": main()
File "C:\Users\administrator\Desktop\Exercise Files\16 Databases\pyodbc.py", line 9, in main
cnxn = pyodbc.connect('DSN=SQL2005;UID=xxx;PWD=xxx')
AttributeError: 'module' object has no attribute 'connect'


Kind regards
Grant aka gunglichen
Last edited by Mekire on Tue Oct 08, 2013 5:14 am, edited 1 time in total.
Reason: First post lock.
gunglichen
 
Posts: 2
Joined: Tue Oct 08, 2013 2:35 am

Re: Pyodbc problem: code not working in IDE: AttributeError

Postby Mekire » Tue Oct 08, 2013 5:19 am

Welcome to the forum.

Not sure if this is your issue here, but did you name another file in your working directory pyodbc? You may be unintentionally importing a module you wrote rather than the intended module. Just a guess though.

-Mek

Edit:
Judging from this part of your traceback:
Code: Select all
File "C:\Users\administrator\Desktop\Exercise Files\16 Databases\pyodbc.py", line 9, in main
this does indeed seem to be the case. Rename your file to something else and it should solve this problem.
User avatar
Mekire
 
Posts: 1147
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Pyodbc problem: code not working in IDE: AttributeError

Postby gunglichen » Tue Oct 08, 2013 7:53 am

Hello Mek,

Wow! Thank you very much. I have changed the name of the test program and it worked as you you suggested.

I never would have thought of that but it makes complete sense to me now.

Thanks again for your time.

Best,
Grant.
gunglichen
 
Posts: 2
Joined: Tue Oct 08, 2013 2:35 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], W3C [Linkcheck] and 7 guests