温馨提示×

python怎么将数据保存到mysql

小新
260
2021-03-22 13:28:52
栏目: 云计算
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

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()

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

推荐阅读:python怎么将列表保存到mysql

0