好吧,这可能是因为很多事情,比如error in the SQL or the table not being found。我们无法判断SQL中是否有错误,因为字符串格式可能会在SQL中插入一些意外的东西(如引号、分号或其他不正确引用的值)。在
有一件事可以解决这个问题,那就是使用2-argument form of ^{},它将为您正确地引用参数。这将更正Prerak Sola指出的引用错误(1如果最后一列是数字类型,则不应将'1'引为'1')sql = """INSERT INTO spin_hdd(
error_count, raw_read_error, spin_up_time, allo_sec, seek_error,
spin_retry, gsense_error, power_on_hours, pending_sector, load_retry,
spin_up_time, max_temp, sector_count, short_test_time,
long_test_time, model, serial, firmware, ip, running)
VALUES ({})""".format(', '.join(['%s']*20))
args = (error_count, raw_read_error, spin_up_time, allo_sec, seek_error,
spin_retry, gsense, power_on, pend_sec, load_retry, spin_up_time,
max_temp, sector_count, testShortTime, testLongTime, model_count,
serial, firmware, ip, 1)
try:
# Execute the SQL command
cur.execute(sql, args)
# Commit your changes in the database
con.commit()
# get last inserted id
id = cur.lastrowid
except:
# Rollback in case there is any error
con.rollback()
e = sys.exc_info()[0]
print e
顺便说一句,您应该始终使用cur.execute的双参数形式,而不是自己手动引用参数,因为这有助于防止SQL injection。在