python - 在python中的mysql表中插入时间戳
问题描述
bid
, best_bid_quantity
are 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()
错误 --> 除非我添加 target_date
解决方案
尝试使用time.strftime()
:
time.strftime('%Y-%m-%d %H:%M:%S', target_date.timetuple())
该.timetuple()
方法需要将 的datetime.datetime
对象target_date
转换为time_struct
结构,然后可以在对 的调用中使用该结构time.strftime
。
推荐阅读
- hibernate - JPA @JoinColumn 与父类的额外条件
- html - 如果 django 模板中的条件不起作用
- python - 如何将 sqlite DB 中的元组转换为 unicode 以更改文本颜色?
- html - 为什么当它们相邻时,margin-left/right 不适用于两个按钮?
- python - 如何使用 python pptx 创建文本形状?
- git - 两个远程仓库之间的 git push
- html - 存在滚动条时,Bootstrap 4 侧边栏列被切断
- docusignapi - DocuSign - 使用 eSignature REST API v2.1 获取签名 URL
- c# - 我正在使用统一制作我的第一个 2d 游戏(我是菜鸟),我试图制作一个死亡屏障,将角色传送回开始
- python - Python Numpy 在两行之间插入一个值