代码之家  ›  专栏  ›  技术社区  ›  DevEx

python在mysql db中插入包含datetime对象的记录时出现问题

  •  0
  • DevEx  · 技术社区  · 6 年前

    我在将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 回复  |  直到 6 年前
        1
  •  0
  •   Ajay Bisht    6 年前

    执行后需要提交

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