码迷,mamicode.com
首页 > 数据库 > 详细

python for mysql

时间:2016-08-31 18:32:06      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

# -*- 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

标签:

原文地址:http://www.cnblogs.com/justdba/p/5826917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!