bid
,best_bid_quantity
是floats
,target_date
在我的数据库中设置为timestamp
。
target_date_time_ms = (k.lastRestRequestTimestamp)
base_datetime = datetime.datetime( 1970, 1, 1 )
delta = datetime.timedelta( 0, 0, 0, target_date_time_ms )
target_date = base_datetime + delta
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="lolilol", # your password
db="test") # name of the data base
cur = db.cursor()
try:
cur.execute("""INSERT INTO test1 (heure, prix, quantite) VALUES ({},{},{})""".format(target_date, bid, best_bid_quantity))
db.commit()
except:
print('did not insert')
db.rollback()
db.close()
答案 0 :(得分:1)
尝试使用time.strftime()
:
time.strftime('%Y-%m-%d %H:%M:%S', target_date.timetuple())
需要使用.timetuple()
方法将datetime.datetime
的{{1}}对象转换为target_date
结构,然后可以将其用于对time_struct
的调用中