2018-07-10

这几天几天刚刚学习python,就动手练习了一下,主要是关于mysql数据库方向的。
我们都知道python的%通配符有:%d(整型),%s(字符型),%f(浮点型)。那么关于时间date的类型呢?
比如:我的项目要往数据库中插入create_time和update_time,那就势必要引用现在的系统时间,经过大量的查找,终于发现往python是没有对应时间datetime的相关通配符的,那么我们要怎么实现呢。

其实很简单,我们只需要把datetime转换成字符串类型的就行

我的代码如下:

def insertIntoChannel(self, user):
        conn = JDBCUtils.getConnection()
        cursor = conn.cursor()
        dt=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        cursor.execute("insert into user(id,age,name,create_time,update_time) \
                      values('%d','%d','%s','%s','%s')" % \
                       (user.getId(),user.getAge(), user.getName(),dt,dt))
        cursor.close()
        conn.commit()
        conn.close()

你可能感兴趣的:(2018-07-10)