私はPythonには新しく、少しタスクを完了するために学んだことを練習すると思っていました。本質的に私はWebから引っ張った.csvファイルからデータベースにコグニティブなカメラデータを挿入しています。残念ながら私はすべての接続の詳細を省略する必要がありました。これは自分の仕事用コンピュータからしかアクセスできないため、スクリプトを実行できません。 問題に! 私は、forループを持っているシステムでは、カメラを反復処理するには、このスクリプトを実行していること:Python forループが1回繰り返して終了する
#!/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
すべては大丈夫です、私は実際に試してみて、データループで何かをするとき、ループはしかし、それが必要として動作します一度実行され、終了すると、これは完全なスクリプトです:
#!/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
問題があり、なぜ私が見ることができないように私は私の頭を悩まれているが、多分私はちょうどそれにいくつかの新鮮な目を必要とします。どんな助けも大きな歓声になるでしょう!
は、あなたが最初のクエリからの結果の完全なセットを取得し、リストにそれらを入れてみました、というよりも反復処理をカーソルオブジェクト?おそらく、ループ内のあなたのデータベース操作は、 'camCursor'の状態を乱しているでしょう。 (基本的に ''リストの行(camCursor)の ''に変更してください: ') – Amber
ええ私もそう思っていますので、リストを取り込み、それが何かをするかどうかを確認する挿入関数に取り組んでいます:)本当に唯一のことですこの時点で私には意味をなさない –
同じ接続で2つのカーソルを使用することで、問題が発生する可能性があります。詳細については、この記事をチェックしてください:http://stackoverflow.com/q/5573724/2112269 – Aquiles