时间:2022-12-06 01:53
python将数据保存到mysql的示例代码:
importMySQLdb#connectMySQL
conn=MySQLdb.connect(
host=,
user=,
passwd=,
db=,
port=,
charset='utf8')
cursor=conn.cursor()
filter_merge_len=len(filter_merge_data)
forxinrange(filter_merge_len):
try:
cursor.execute(
"insertintodatabase_name.table_name(date,monitor_item,host_ip,cluster\
,software,alarm_hit)values('%s','%s','%s','%s','%s','%d')"%
(yest_date,filter_merge_data.iloc[:,0][x],
filter_merge_data.iloc[:,1][x],
filter_merge_data.iloc[:,2][x],
filter_merge_data.iloc[:,3][x],
filter_merge_data.iloc[:,4][x]))
#提交到数据库执行
conn.commit()
exceptExceptionase:
print'InsertFailed'
#close
conn.commit()
cursor.close()
conn.close()