INSERT INTO [表名] (time) VALUES ('2019-11-07 01:51')
INSERT INTO [表名] (time) VALUES ('2019-11-07 01:51:03')
import datetime
if __name__ == "__main__":
t1 = datetime.datetime.now()
t2 = datetime.datetime.utcnow()
print('当前时间:', t1)
print('世界时间:', t2)
t3 = datetime.datetime(2019, 11, 11, 20, 45, 45)
print('指定日期:', t3)
print('转化为:')
t4 = datetime.datetime.strptime('2019/11/11', '%Y/%m/%d')
print(t4)
t5 = datetime.datetime.strptime('2019年11月11日', '%Y年%m月%d日')
print(t5)
t6 = datetime.datetime.strptime('2019年11月11日20时45分45秒', '%Y年%m月%d日%H时%M分%S秒')
print(t6)
t7 = datetime.datetime.strptime('11/11/2019', '%m/%d/%Y')
print(t7)
t8 = datetime.datetime.strptime('11/11/2019 20:45:45', '%m/%d/%Y %H:%M:%S')
print(t8)
t9 = datetime.datetime(2019, 11, 11, 20, 45, 45)
print('转化为:')
print(t9.strftime('%Y{y}%m{m}%d{d}%H{h}%M{M}%S{s}').format(y='年', m='月', d='日', h='时', M='分', s='秒'))
print(t9.strftime('%m/%d/%Y %I:%M:%S%p'))
print(t9.strftime('%Y-%m-%d'))
t10 = datetime.datetime.now()
w = t10.strftime('%w')
j = t10.strftime('%j')
W = t10.strftime('%W')
d = t10.strftime('%d')
print('今天是这周的第{}天'.format(w))
print('今天是今年的第{}天'.format(j))
print('今周是今年的第{}周'.format(W))
print('今天是当月的第{}天'.format(d))
today_time = str(datetime.datetime.now()).split('.')[0]
print(today_time)
today_time = datetime.datetime.strptime(today_time, '%Y-%m-%d %H:%M:%S')
sql = "INSERT INTO t_train_number (time) VALUES ('%s')" % today_time
print(sql)
一、MySQL中记录时间的类型二、python处理日期和时间(全部代码在最后)使用的是 datetime 库处理时间python中时间日期格式化符号:%y 两位数的年份表示(00-99)%Y 四位数的年份表示(000-9999)%m 月份(01-12)%d 月内中的一天(0-31)%H 24小时制小时数(0-23)%I 12小时制小时数(01-12) %M 分钟数(00=59...
python3.7.4版本,文件包含excel文件和py文件。
py文件中需要手动设置excel字段在mysql中的类型、index索引及写入时校验的字段。(搜索*查找对应的位置)
执行py文件,若不存在数据库及表会自动创建,并写入数据(对于指定字段重复的不会写入)
import pymysql
#import datetime
#day = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')#参数值插入时间
db = pymysql.connect(host='\u670d\u52a1\u5668IP', user='\u8d26\u53f7', passwd='\u5bc6\u7801', port=端口号)
cur = db.cursor()
cur.execute('use 数据库')
#批量创建测试账号
import datetime #依赖
data_time=datetime.datetime.now().strftime("%Y-%m-%d%H:%M:%S") #系统时间
sql = "INSERT INTO b_s(s_id,p_id,time) VALUES (%s, %s, %s)" # sql
cursor.execute(sql, (sid, pid, data_time)) #添加参数
#时间 2019/06/21
print "##############################################"
print "# 在执行此脚本前请先执行 #"
pri...
刚开始使用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
#------看我写的程序-------
可以使用Python中的datetime模块来创建时间类型的数据,并使用MySQL的INSERT语句将其插入到数据库中。以下是一个示例代码:
```python
import mysql.connector
from datetime import datetime
# 连接到数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
# 创建一个游标对象
mycursor = mydb.cursor()
# 创建一个包含时间类型的数据
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
data = ('John', 'Doe', formatted_date)
# 插入数据到数据库
sql = "INSERT INTO customers (first_name, last_name, registration_date) VALUES (%s, %s, %s)"
mycursor.execute(sql, data)
# 提交更改
mydb.commit()
# 打印成功插入的行数
print(mycursor.rowcount, "record inserted.")
在上面的示例中,我们使用了datetime.now()函数创建了一个时间类型的数据,然后使用了strftime()函数将其格式化为MySQL所支持的日期时间格式。最后,我们使用了MySQL的INSERT语句将数据插入到数据库中。