python for mysql
Posted 两年十一时
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章