python3实现往mysql中插入datetime类型的数据


Posted in Python onMarch 02, 2020

昨天在这个上面找了好久的错,嘤嘤嘤~

很多时候我们在爬取数据存储的时候都需要将当前时间作为一个依据,在python里面没有时间类型可以直接拿来就用的。我们只需要在存储之前将时间类型稍作修饰就行。

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

如:

#插入产品信息
 insert_good_sql = """
 INSERT INTO T_GOOD(good_name, good_type, img_src, good_description, how_to_use, volumetric, price,sale, spider_time)
 VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)
 """
 values = (pymysql.escape_string(data_dict['good_name']), pymysql.escape_string(data_dict['good_type']),
 data_dict['img_src'], pymysql.escape_string(data_dict['good_description']), data_dict['how_to_use'],
 pymysql.escape_string(data_dict['volumetric']), pymysql.escape_string(data_dict['price']),
 data_dict['sale'], datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
 cursor.execute(insert_good_sql, values)

补充拓展:谈谈python写入mysql中datetime类型遇到的问题

刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题:

在mysql数据库中,有一个datetime类型的字段用于存储记录的日期时间值。python程序中有对应的一个datetime变量dt。

现在需要往mysql数据库中添加记录,每次添加时,将datetime型变量dt写入mysql数据库tablename表中exTime字段里。

问题,如何写入?调试时,总是无法写入。

运行环境:windows10 python 3.6 mysql5.6.38

运行结果提示:

Process finished with exit code 0

#------看我写的程序-------------
import datetime
import pymysql.cursors
conn = pymysql.connect(host='127.0.0.1',
 port=3306,
 user='root',
 password='',
 db='test',
 charset='utf8',
 cursorclass=pymysql.cursors.DictCursor)
#中间略去dt赋值部分...
print(dt.strftime('%Y-%m-%d %H:%M:%S'))
#运行结果是 2001-1-2 11:00:00
sql_insert=sql_insert="INSERT into tablename(exTime) values(%s)" %(dt.strftime("%Y-%m-%d %H:%M:%S"))
#如果此处写成sql_insert=sql_insert="INSERT into tablename(exTime) values('2001-1-2 11:00:00')" 则可以运行

try:
 with conn.cursor() as csor1:
 
 csor1.execute(sql_insert)
 conn.commit()
 csor1.close()
except Exception as e:
 #错误回滚
 conn.rollback()
finally:
 conn.close()
###-----------------------------------

后来在网上查了一下,mysql中datetime类型字段,赋值时最好用str_to_date函数转化成mysql的datetime类型

因此,把上面的程序改了一下:

sql_insert=sql_insert="INSERT into tablename(exTime) values(str_to_date(\'%s\','%%Y-%%m-%%d %%H:%%i:%%s'))" %(dt.strftime("%Y-%m-%d %H:%M:%S"))

重新运行,通过!

以上这篇python3实现往mysql中插入datetime类型的数据就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python连接oracle数据库实例
Oct 17 Python
Python字符串处理函数简明总结
Apr 13 Python
python使用自定义user-agent抓取网页的方法
Apr 15 Python
python中管道用法入门实例
Jun 04 Python
Python合并字典键值并去除重复元素的实例
Dec 18 Python
Python实现屏幕截图的两种方式
Feb 05 Python
Python中Numpy包的安装与使用方法简明教程
Jul 03 Python
python基于SMTP协议发送邮件
May 31 Python
使用Python的Turtle绘制哆啦A梦实例
Nov 21 Python
解决torch.autograd.backward中的参数问题
Jan 07 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
Python中的面向接口编程示例详解
Jan 17 Python
python3将变量写入SQL语句的实现方式
Mar 02 #Python
Python *args和**kwargs用法实例解析
Mar 02 #Python
Python通过2种方法输出带颜色字体
Mar 02 #Python
Python实现屏幕录制功能的代码
Mar 02 #Python
python实现录屏功能(亲测好用)
Mar 02 #Python
基于Numba提高python运行效率过程解析
Mar 02 #Python
Python3 assert断言实现原理解析
Mar 02 #Python
You might like
支持oicq头像的留言簿(二)
2006/10/09 PHP
海河写的 Discuz论坛帖子调用js的php代码
2007/08/23 PHP
PHP中替换换行符的几种方法小结
2012/10/15 PHP
php删除数组元素示例分享
2014/02/17 PHP
py文件转exe时包含paramiko模块出错解决方法
2016/08/12 PHP
Lumen timezone 时区设置方法(慢了8个小时)
2018/01/20 PHP
PHP addcslashes()函数讲解
2019/02/03 PHP
再次更新!MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类 Ver 1.6)
2007/02/05 Javascript
Js nodeType 属性全面解析
2013/11/14 Javascript
jquery ajax跨域解决方法(json方式)
2014/02/04 Javascript
详解JavaScript中void语句的使用
2015/06/04 Javascript
JS实现的3D拖拽翻页效果代码
2015/10/31 Javascript
BootStrap Validator 根据条件在JS中添加或移除校验操作
2017/10/12 Javascript
Vue-CLI 3.X 部署项目至生产服务器的方法
2019/03/22 Javascript
LayUI switch 开关监听 获取属性值、更改状态的方法
2019/09/21 Javascript
ES6使用 Array.includes 处理多重条件用法实例分析
2020/03/02 Javascript
Python中类的定义、继承及使用对象实例详解
2015/04/30 Python
python和bash统计CPU利用率的方法
2015/07/10 Python
详细介绍Python的鸭子类型
2016/09/12 Python
Python中matplotlib中文乱码解决办法
2017/05/12 Python
python unittest实现api自动化测试
2018/04/04 Python
pandas对dataFrame中某一个列的数据进行处理的方法
2019/07/08 Python
使用 Python 快速实现 HTTP 和 FTP 服务器的方法
2019/07/22 Python
python 实现方阵的对角线遍历示例
2019/11/29 Python
Python json转字典字符方法实例解析
2020/04/13 Python
python数据处理——对pandas进行数据变频或插值实例
2020/04/22 Python
python编程的核心知识点总结
2021/02/08 Python
联想墨西哥官方网站:Lenovo墨西哥
2016/08/17 全球购物
介绍一下Ruby的多线程处理
2013/02/01 面试题
软件工程师岗位职责
2013/11/16 职场文书
自我鉴定写作要点
2014/01/17 职场文书
中学生自我评价范文
2014/02/08 职场文书
年底个人总结范文
2015/03/10 职场文书
2015秋季开学典礼主持词
2015/07/16 职场文书
PHP实现rar解压读取扩展包小结
2021/06/03 PHP