将python时间对象插入MySQL表

将python时间对象插入MySQL表,python,mysql,mysql-python,Python,Mysql,Mysql Python,我有一个MySQL数据库。我使用pythonmysqldb模块访问这个数据库。我有一个表,它有一个mysql数据类型为time的列 我应该使用哪种python数据类型在该列中插入时间值?我应该在execute语句中使用什么?据我所知,您可以使用datetime.time、datetime.datetime或字符串作为数据类型 假设您已经设置了一个游标,那么如果您只是向表中添加时间,那么这是一个非常简单的查询 cursor.execute('''insert into table my_table

我有一个MySQL数据库。我使用python
mysqldb
模块访问这个数据库。我有一个表,它有一个mysql数据类型为
time
的列


我应该使用哪种python数据类型在该列中插入时间值?我应该在execute语句中使用什么?

据我所知,您可以使用
datetime.time
datetime.datetime
或字符串作为数据类型

假设您已经设置了一个游标,那么如果您只是向表中添加时间,那么这是一个非常简单的查询

cursor.execute('''insert into table my_table (time_column) values (%s)''', '2022-12-25 10:32:23')
cursor.execute('''insert into table my_table (time_column) values (%s)''', '10:32:23')
cursor.execute('''insert into table my_table (time_column) values (%s)''', datetime.time(10,32,23)
cursor.execute('''insert into table my_table (time_column) values (%s)''', datetime.datetime(2022,12,25,10,32,23)))
所有这些都将导致将
10:32:23
的时间添加到MySQL表
my\u表
中标记为
time\u列的列中