如果参数从未遇到过python脚本

时间:2014-07-10 02:09:51

标签: python

我有以下python脚本,每次运行都会更新数据库,即使256sha是相同的。

import os
import hashlib
import pynuodb

connection = pynuodb.connect("CORE", "priappvmndb01", "ndbadmin", "B1xwmi28", options={'schema': 'markit'})
cursor = connection.cursor()
filename = 'BOND_COMPOSITES_20140624.7z'
thedata = pynuodb.Binary(open(filename, 'rb').read())
with open(filename,"rb") as f:
    for line in f.readlines():
        line = line.rstrip("\n")

        m = hashlib.sha256(line)
        sha256 = (m.hexdigest())
sqlget = "select md5hash from raw_data where filename = ?"
sqlins = "insert into raw_data (genericfilename, fileid, fileformat, loaddate, revisionnumber, rawdata, md5hash, filename) values ('bond_composites.csv', 1, 'csv', 'now', 1, ?, ?, ?)"
cursor.execute(sqlget, (filename,))
result = cursor.fetchone()
if sha256 == result:
    print "Nothing to be done."
else:
    cursor.execute(sqlins, (thedata, sha256, filename))
    connection.commit()
    print "Database Updated!"

我从中得出问题在于我的问题是否意味着它可能无法识别其中一个变量?

1 个答案:

答案 0 :(得分:2)

python DB-API返回该行的元组,即使它只包含一个值。

'foo' != ('foo',)

所以你可能想做的是:

if sha256 == result[0]: