Linux 拨号vps windows公众号手机端

python怎么将数据保存到mysql

lewis 7年前 (2018-09-17) 阅读数 12 #VPS/云服务器

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


版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门