首页 > 代码库 > python for mysql
python for mysql
# -*- coding: utf-8 -*-
‘‘‘
python coded by
written in 2016/8/31
Used for get win os log for each windows server
‘‘‘
‘‘‘
pymssql 帮助文档
http://pymssql.org/en/stable/pymssql_examples.html
‘‘‘
import pymssql
import MySQLdb
import time
#全局变量
host = "sqlmonitorex.mysql.db.aa.com"
user = "aa"
password = "aa"
dbname = "testdb"
port = 555
def fetch_row():
try:
#conn = pymssql.connect(host,port,user,password,dbname,charset="UTF-8",timeout=3)
conn = MySQLdb.connect(host,user,password,dbname,port=port,charset="utf8",connect_timeout=3)
cursor = conn.cursor()
sql = "select * from temp1;"
cursor.execute(sql)
row = cursor.fetchone() #相当于 cursor.next方法
while row:
print row[0],row[1] #格式化输出
#time.sleep(1) #每次输出等待1秒
row = cursor.fetchone() #输出下一行 cursor.next方法
conn.close()
return 1
except Exception,e:
return e
def change_row():
try:
conn = MySQLdb.connect(host,user,password,dbname,port=port,charset="utf8",connect_timeout=3)
cursor = conn.cursor()
#sql="insert into temp1 (id,name)values (1,‘xxx‘)"
sql="update temp1 set name=‘ZZZ‘ where id=1 "
cursor.execute(sql)
conn.commit()
conn.close()
return 1
except Exception,e:
conn.rollback()
return e
i=fetch_row()
print i
change_row()
python for mysql