下载模块
pip3 install pymysql
python脚本
import pymysql
li = []
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='123456',
db='host_info',
charset='utf8mb4'
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
base_info = [{
"manufacturer": "VMware, Inc.",
"pod_name": "VMware7,1",
"sn": "VMware-56 4d 2d 4c 91 1e 48 15-5b d2 73 9c ec 98 da 22",
"host_name": "sharkyum.com",
"kernel": "3.10.0-957.el7.x86_64",
"os": "CentOS Linux release 7.6.1810 (Core)",
"cpu_name": "Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz"},
{
"manufacturer": "VMware, Inc.",
"pod_name": "VMware7,1",
"sn": "VMware-56 4d 2d 4c 91 1e 48 15-5b d2 73 9c ec 98 da 22",
"host_name": "sharkyum.com",
"kernel": "3.10.0-957.el7.x86_64",
"os": "CentOS Linux ",
"cpu_name": "Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz"}]
sql = '''
insert into base_info(
manufacturer, pod_name,
sn, host_name,
kernel, os, cpu_name)
values(%s, %s, %s, %s, %s, %s, %s);'''
def foo():
for i in base_info:
infos = tuple(i.values())
li.append(infos)
cursor.executemany(sql, li)
conn.commit()
cursor.close()
conn.close()
foo()