0

I am new to python and thought I would practice what I have been learning to complete a little task. Essentially I am inserting a cognimatic cameras data into a database from a .csv file that I pulled from the web. sadly I have had to omit all the connection details as it can only be accessed from my works computer which means the script cannot be run. To the problem! I have a for loop that iterates through the cameras in the system, running this script:

 #!/usr/bin/python
import pymssql
import urllib2
import sys
import getpass
import csv
import os

attempts = 0 #connection attempt counter

#check db connection with tsql -H cabernet.ad.uow.edu.au -p 1433 -U ADUOW\\mbeavis -P mb1987 -D library_gate_counts

server = "*****" #sever address
#myUser = 'ADUOW\\' + raw_input("User: ")#  User and password for server. Will this be needed when the script runs on the server? # Ask David
#passw = getpass.getpass("Password: ")          

while attempts < 3: # attempt to connect 3 times
    try: #try connection
        conn = pymssql.connect(server = server, user = '****', password = '****', database = "***", port='1433',timeout = 15, login_timeout = 15)
        break
    except pymssql.Error as e: #if connection fails print error information
        attempts += 1
        print type(e)
        print e.args

camCursor = conn.cursor() #creates a cursor on the database

camCursor.execute("SELECT * FROM dbo.CAMERAS") #Selects the camera names and connection details 



for rows in camCursor:
    print rows

Everything is fine and the loop runs as it should, however when I actually try and do anything with the data the loop runs once and ends, this is the full script:

    #!/usr/bin/python
import pymssql
import urllib2
import sys
import getpass
import csv
import os

attempts = 0 #connection attempt counter

#check db connection with tsql -H cabernet.ad.uow.edu.au -p 1433 -U ADUOW\\mbeavis -P mb1987 -D library_gate_counts

server = "*****" #sever address
#myUser = 'ADUOW\\' + raw_input("User: ")#  User and password for server. Will this be needed when the script runs on the server? # Ask David
#passw = getpass.getpass("Password: ")          

while attempts < 3: # attempt to connect 3 times
    try: #try connection
        conn = pymssql.connect(server = server, user = '****', password = '****', database = "***", port='1433',timeout = 15, login_timeout = 15)
        break
    except pymssql.Error as e: #if connection fails print error information
        attempts += 1
        print type(e)
        print e.args

camCursor = conn.cursor() #creates a cursor on the database

camCursor.execute("SELECT * FROM dbo.CAMERAS") #Selects the camera names and connection details 



for rows in camCursor:
    print rows
    cameraName = str(rows[0]) #converts UNICODE camera name to string
    connectionDetails = str(rows[1]) #converts UNICODE connection details to string

    try: #try connection
        #connect to webpage, this will be changed to loop through the entire range of cameras, which will
        #have their names and connection details stored in a seperate database table
        prefix = "***"
        suffix = "**suffix"
        response = urllib2.urlopen(prefix + connectionDetails + suffix, timeout = 5)
        content = response.read() #read the data for the csv page into content
        f = open( "/tmp/test.csv", 'w' ) #open a file for writing (test phase only)
        f.write( content ) #write the data stored in content to file
        f.close() #close file
        print content #prints out content
        with open( "/tmp/test.csv", 'rb' ) as csvFile: #opens the .csv file previously created
            reader = csv.DictReader(csvFile) #reader object of DictReader, allows for the first row to be the dictionary keys for the following rows
            for row in reader: #loop through each row
                start = row['Interval start']
                end = row['Interval stop']
                camName = row['Counter name']
                pplIn = int(row['Pedestrians coming in'])
                pplOut = int(row['Pedestrians going out'])
                insertCursor = conn.cursor()
                insert = "INSERT INTO dbo.COUNTS VALUES (%s, %s, %d, %d)"
                insertCursor.execute(insert, (camName, start, pplIn, pplOut))
                conn.commit()
    except urllib2.URLError as e: #catch URL errors
        print type(e)
        print e.args
    except urllib2.HTTPError as e: #catch HTTP erros
        print type(e)
        print e.code

I have been scratching my head as I cannot see why there is a problem, but maybe I just need some fresh eyes on it. Any help would be great cheers!

M.Beavis
  • 3
  • 1
  • 1
    Have you tried retrieving the full set of results from the initial query and putting them into a list, rather than iterating over the cursor object? Perhaps your database operation within the loop is messing with `camCursor`'s state. (Basically change to `for rows in list(camCursor):`) – Amber Jun 30 '16 at 02:56
  • yeah I thought that as well so I am now working on an insertion function that takes the list and see if that does any thing :) It really is the only thing that makes sense to me at this point – M.Beavis Jun 30 '16 at 02:59
  • The problem might be related to you using two cursors on the same connection. Check this post for more information: http://stackoverflow.com/q/5573724/2112269 – Aquiles Jun 30 '16 at 03:06

1 Answers1

0

Have you tried to do something like

queryResult = camCursor.execute("SELECT * FROM dbo.CAMERAS")

for rows in queryResult:
    ...

I guess this might solve the problem, which is probably the fact that you're trying to iterate over a cursor instead of the results.

You might find this way interesting as well:

camCursor.execute("SELECT * FROM dbo.CAMERAS")

for rows in camCursor.fetchall():
    ...

Source: https://docs.python.org/2/library/sqlite3.html

igorcadelima
  • 136
  • 9
  • 1
    thats it!!! camCursor.execute("SELECT * FROM dbo.CAMERAS") #Selects the camera names and connection details queryResult = camCursor.fetchall() for rows in queryResult: .... that worked like a charm thank you so much – M.Beavis Jun 30 '16 at 03:19