0

I am using Python library pymssql to connect to a database. I have never used it before so the error might look trivial.

from os import getenv
import pymssql

server = getenv("192.xxx.xxx.xx") 
user = getenv("john.constantine")
password = getenv("xxxxxxx")

conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()

cursor.execute('SELECT * from table')
rows = cursor.fetchall()
for row in rows:
    print row

conn.close()

I get the following error:

Traceback (most recent call last):
  File "C:\Users\Documents\Demo_DB.py", line 8, in <module>
    conn = pymssql.connect(server, user, password, "tempdb")
  File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
  File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
  File "_mssql.pyx", line 552, in _mssql.MSSQLConnection.__init__ (_mssql.c:5891)
TypeError: argument of type 'NoneType' is not iterable
[Finished in 0.2s with exit code 1]

I connect to DB using these credentials. enter image description here

John Constantine
  • 1,038
  • 4
  • 15
  • 43
  • Your table in `tempdb` is called `table`? – Shawn Mehan Jun 01 '17 at 23:34
  • @ShawnMehan The Table is under "Databases" – John Constantine Jun 01 '17 at 23:37
  • 1
    `server = getenv("192.xxx.xx.xx")` is going to set `server` to `None`, unless you actually have an environment variable named `192.xxx.xx.xx`. That's probably what's given you the None which is throwing the exception. (Same for your other `getenv`.) – pbuck Jun 01 '17 at 23:38
  • @ShawnMehan I have updated the question. That's how I connect to the DB. – John Constantine Jun 01 '17 at 23:42
  • @pbuck probably has it. print out the values that you get back from the `getenv`. Otherwise, you can place a break point in the init. I've been looking at the [code](https://github.com/pymssql/pymssql/blob/master/src/_mssql.pyx) and I would step through it if it ain't a missing envvar – Shawn Mehan Jun 01 '17 at 23:46
  • 1
    If you know the server address is `192.111.111.11`, why on earth are you calling `getenv()`? Just do `server = '192.111.111.11'` and be done with it. – John Gordon Jun 01 '17 at 23:47
  • I was trying the exact thing from documentation. It worked. Thanks @JohnGordon – John Constantine Jun 01 '17 at 23:50
  • https://github.com/PyMySQL/PyMySQL/blob/master/example.py – SteveJ Jun 02 '17 at 03:36

0 Answers0