Python连接MySQL数据库

#!/usr/bin/env python
import MySQLdb
import os
import subprocess
import pipes
from subprocess import *
import datetime
ssh_host = "[email protected]"
resp = Popen( ['ssh',ssh_host, 'df -h'],stdout=PIPE,stdin=PIPE)
s = resp.stdout.read()
L = s.splitlines()
date=datetime.datetime.now().strftime('%Y-%m-%d')
for i in range(1,len(L)) :
	l = L[i].split()
	value={"NAS_name":l[0],"size":l[1],"used":l[2],"Avail":l[3],"use_num":l[4],"mounted_on":l[5],"Date":date}
	db = MySQLdb.connect("localhost", "root", "zxcv=1234", "nas", charset='utf8' )
	cursor = db.cursor()
	sql = """INSERT INTO NAS VALUES(%(NAS_name)s, %(size)s, %(used)s, %(Avail)s, %(use_num)s, %(mounted_on)s, %(Date)s)"""
	cursor.execute(sql,value)
	db.commit()
	#db.rollback()
	db.close()`

你可能感兴趣的:(python,数据库)