0
psycopg2を使用してpythonスクリプトを作成し、新しいデータを挿入するか、postgresqlデータベースにデータを更新します。 hourly_count
はユーザによって入力され、整数が挿入されると完全に機能します。文字列が試されても何も入力しないことが重要ですが、null入力を処理できるようにする必要があります。今はnull入力は以下のUnboundLocalErrorが発生しますUnboundLocalErrorを引き起こすヌルユーザ入力
UnboundLocalError: local variable 'hourly_count' referenced before assignment
私が正しく理解していれば、hourly_count = int(ped[time]) * 6
のint()関数はNULL入力を防止するものです。
文字列を許可せずにnull入力を許可するには、どうすればよいですか?
def insert_ped(request_json):
try:
ped = request_json['counts']['ped']
for time in ped:
time = time
hourly_count = int(ped[time]) * 6
q = """insert into table (
time,
hourly_count
) values (
%s,
%s
)"""
con = make_con()
cur = con.cursor()
cur.execute(q, (
time,
hourly_count
))
con.commit()
except Exception, e:
print e
yyyy_mm_dd=request_json['counts']['attributes']['date']
q = """update table set
time = %s,
hourly_count = %s"""
con = make_con()
cur = con.cursor()
cur.execute(q, (
time,
hourly_count
))
con.commit()
これは、ベース10と)(UnboundLocalError問題を解決したが、今私はint型のリテラル '無効を与える: '''私は私が意味するのにかかるどの」 mはまだint()関数のpythonの規則に従っています。 – Kasey
さて、あなたがそれを使用する前に辞書 'ped 'が実際に' time'項目を持っているかどうかをチェックする必要があります: 'when in ped:... int(time [ped])...' – DyZ