
要正在 MySQL 数据库外拔出日期,你的表外须要有一列范例为日期或者日期光阴的列。实现后,你需求将日期转换为字符串款式,而后再将其拔出数据库。为此,你可使用 datetime 模块的 strftime 格局化函数。
事例
from datetime import datetime now = datetime.now() id = 1 formatted_date = now.strftime('%Y-%m-%d %H:%M:%S') # Assuming you have a cursor named cursor you want to execute this query on: cursor.execute('insert into table(id, date_created) values(%s, %s)', (id, formatted_date))
登录后复造
运转此号召将测验考试将元组(id,date)拔出到你的表外。
当你应用select盘问从数据库外猎取日期时,你需求利用strptime等函数将其解析归datetime器械。
事例
from datetime import datetime # Assuming you have a cursor named cursor you want to execute this query on: cursor.execute('select id, date_created from table where id=1') # if you inserted the row above, you can get it back like this id, date_str = cursor.fetchone() # date is returned as a string in format we sent it as. So parse it using strptime created_date = datetime.strptime(date_created, '%Y-%m-%d %H:%M:%S')
登录后复造
那将猎取建立日期并将其解析为日期光阴器械。
以上即是怎样利用 Python 正在 MySQL 数据库外存储以及检索日期?的具体形式,更多请存眷萤水红IT仄台此外相闭文章!

发表评论 取消回复