利用pymysql往数据库中添加时间字段的方法:
1.导入datetime和pymysql:
安装:pip install pymysql, datetime是内置
2.写入SQL语句:
向对应的字段中间添加对应的数据,全部用%s后面再将所有的内容集成到一个list中然后用【cursor.excutemany(sql, list)】,将所有的数据全部执行进去。最后【connect.commit()】确认提交
1 def store_data(self, user_id, spo_list, industry="csv"):
2 """
3
4 :param user_id: id 信息 int
5 :param spo_list: 三元组 二维嵌套数组
6 :param industry: 类别
7 :return:
8 """
9 sql = "INSERT INTO triads(user_id, entity_one, relation, entity_two, industry, created_at) VALUES (%s, %s, %s, %s, %s, %s)"
10 try:
11 combine = []
12 for i in spo_list:
13 combine.append([str(user_id)] + i + [industry] + [datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')])
14 self.cursor.executemany(sql, combine)
15 self.connect.commit()
16 return True
17 except Exception as e:
18 print(e)
19 self.connect.rollback()
20 return False
21 finally:
22 self.end()