python怎么将数据保存到mysql
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()
声明:本站所有作品(图文、音视频)均由用户自行上传分享,或互联网相关知识整合,仅供网友学习交流,若您的权利被侵害,请联系 管理员 删除。
本文链接:http://news.ew35.com/