python将包含datetime对象的记录插入mysql数据库的问题

时间:2018-08-20 17:22:15

标签: python mysql

我在将datetime对象插入mysql时遇到问题。没有错误信息,但看不到插入的记录。我打印了插入的输出,它返回了1

now = datetime(2018, 8, 20, 11, 5, 3)
print now

conn = MySQLdb.connect(host=hostname, user=username, passwd=password, db=database)
cur = conn.cursor()
d = cur.execute('INSERT INTO mytable(begin_date, end_date, status) VALUES (%s,%s,%s)',(now,now,"success"))
print d
>>
2018-08-20 11:05:03
1


mysql> describe mytable;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| begin_date | datetime     | YES  |     | NULL    |                |
| end_date   | datetime     | YES  |     | NULL    |                |
| status     | varchar(200) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:0)

您需要在执行后提交

# Make sure data is committed to the database
 conn.commit()

 cur.close()
 conn.close()