请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

请问datatime类型的如何插入

sql = “INSERT news(title,content,types,image,authorcreate_at) VALUE (%s,%s,%s,%s,%s,%s))“
cursor = self.conn.cursor()
dt = datetime.datetime.now().strftime(”%Y-%m-%d %H:%M:%S”)
cursor.execute(sql,(‘python’,‘python11111’,‘学习’,‘图片地址1’,‘作者python’,dt))

正在回答

2回答

另外,如果你想插入当前时间的话,可以直接使用mysql中的now()函数,SQL示例:

sql = (
                "INSERT INTO `news`(`title`,`image`, `content`, `types`, `is_valid`, `created_at`) VALUE"
                "(%s, %s, %s, %s, %s, now());"
            )


0 回复 有任何疑惑可以回复我~
NavCat 2020-02-25 14:55:57

你这种方式是可以插入的,有报错吗?贴出来看看

0 回复 有任何疑惑可以回复我~
  • 提问者 慕运维5654693 #1
    卡了。。
    回复 有任何疑惑可以回复我~ 2020-02-25 21:19:29
  • 提问者 慕运维5654693 #2
    卡了。。。
    回复 有任何疑惑可以回复我~ 2020-02-25 21:19:33
  • 提问者 慕运维5654693 #3
    <class 'datetime.datetime'>
    (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',`create_at`) VALUE ('python','python11111','学习','图片地址1','作者py' at line 1")
    老师,我上网查了一下,python的通配符好像不支持datetime类型
    回复 有任何疑惑可以回复我~ 2020-02-25 21:19:40
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号