python将数据保存到mysql的示例代码:
import MySQLdb# connect MySQL
conn = MySQLdb.connect(
host= ,
user= ,
passwd= ,
db= ,
port= ,
charset='utf8')
cursor = conn.cursor()
filter_merge_len = len(filter_merge_data)
for x in range(filter_merge_len):
try:
cursor.execute(
"insert into database_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()
except Exception as e:
print 'Insert Failed'
# close
conn.commit()
cursor.close()
conn.close()