python-创建跟日期相关的表datetime
db = pymysql.connect(host=mysqlHost, port=mysqlPort, user=mysqlUser, passwd=mysqlPwd, db=mysqlSchema, charset='utf8') cursor = db.cursor() time_now = datetime.datetime.now()#获取当前时间 for i in range(0,120):# 当前时间偏移 time_table_pre =time_now + datetime.timedelta(days=i)#偏移时间 time_table_pre = time_table_pre.strftime("%Y%m%d") #转化格式为20221101 create_table_sql = "创建表sql,表名为日期后缀".format(time_table_pre) cursor.execute(create_table_sql)
db.commit()
cursor.close()
db.close()
import pymysql db = pymysql.connect(host=mysqlHost, port=mysqlPort, user=mysqlUser, passwd=mysqlPwd, db=mysqlSchema, charset='utf8') cursor = db.cursor() this_sql = "INSERT INTO biaoming(lieming, lieming, lieming,) VALUES " id = 4897 ch_id=1174 for i in range(101000): #拼接插入的sql this_sql += "('{}', '{}', '2022-09-21 00:00:00', '2', '10.35.2.111', '2022-09-21 14:09:01', '2022-09-21 18:19:38', '100.00', '15037000', '2022-09-21 18:19:38', '测试脏数据'),"\ .format( id+i,ch_id+i) this_sql = this_sql[:-1] #去掉最后拼接的都好, cursor.execute(this_sql) db.commit() cursor.close() db.close()
常用的:
1、datetime. strftime (format):返回自定义格式化字符串。
2、时间偏移:datetime.timedelta类
timedelta表示时间的差值对象
语法:datetime.timedelta(weeks=0,days=0,hours=0,minutes=0,seconds=0,milliseconds=0,microsecons=0)
每个参数可正,可负也可0,默认是0
3、获取当前日期和时间:datetime.datetime.now()
4、获取当前日期 datetime.date.today()
5、dir()函数来获取包含模块所有属性的列表,
print(dir(datetime))
datetime模块定义了5个类
datetime.date:表示日期的类,常用的属性有year, month, day
datetime.datetime:表示日期时间的类,常用的属性有hour, minute, second, microsecond
datetime.time:表示时间的类
datetime.timedelta:表示时间间隔,即两个时间点的间隔。在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算
datetime.tzinfo:时区的相关信息
6、时间戳转化为日期:
from datetime import date
timestamp = date.fromtimestamp(1576244364)

浙公网安备 33010602011771号