python mysql数据库插入timestamp
Posted peach_li
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python mysql数据库插入timestamp相关的知识,希望对你有一定的参考价值。
利用python向mysql数据库插入timestamp
def insert_install_service_info(serviceName,nodeID,nodeIP='9.42.83.54'):
conn = MySQLdb.connect(host='172.17.46.15',user='root',passwd='huxin2010')
curs = conn.cursor()
serviceNode =
try:
conn.select_db('nodeInfo')
except Exception:
logger.error("the database you find does not exist!")
try:
sql = "insert into service3 values('NULL',%s,%s,%s,%s)"
now = datetime.datetime.now()
now = now.strftime("%Y-%m-%d %H:%M:%S")
values = [serviceName,nodeID,nodeIP,now]
curs.execute(sql,values)
except Exception:
logger.error("error: insert data fail")
conn.commit()
curs.close()
conn.close()
数据表定义如下:
mysql> describe service3;
+-------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------------------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| serviceName | varchar(20) | NO | | NULL | |
| nodeID | int(4) | NO | | NULL | |
| nodeIP | varchar(20) | NO | | NULL | |
| time | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------+-------------+------+-----+---------------------+----------------+
5 rows in set (0.26 sec)
以上是关于python mysql数据库插入timestamp的主要内容,如果未能解决你的问题,请参考以下文章