简体   繁体   中英

Python for loop dropping after 1 iteration

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM