python 3 简单线程、数据库操、Mssql访问示例
'''
Tested:
Python 3.3.0
Microsoft SQL Server 2008
'''
import time
import random
import adodbapi
import threading
'''
简单定时器类
'''
class Timer(threading.Thread):
def __init__(self,fn,args=(),sleep = 0):
threading.Thread.__init__(self)
self.fn = fn
self.args = args
self.interval = sleep
self.setDaemon(True)
self.enable = True
self.running = False
def __do(self):
self.running = True
self.fn(*self.args)
self.running = False
def run(self):
while self.enable :
time.sleep(self.interval)
self.__do()
def stop(self):
#stop the loop
self.enable = False
while True:
if not self.running : break
time.sleep(0.01)
'''
简单数据库访问类
'''
class DbHelper:
def __init__(self,conn):
self.conn = conn;
try:
self.connect = adodbapi.connect(conn,120)
self.cursor = self.connect.cursor();
except EnvironmentError as err:
print('connection failure:' + err);
def getConn(self):
return self.conn;
def getDbTime(self):
self.cursor.execute("select getdate()");
daterow = self.cursor.fetchone();
return str(daterow[0]);
def execNoResult(self,sql):
self.cursor.execute(sql);
self.connect.commit();
def getExecResult(self,sql):
self.cursor.execute(sql)
return self.cursor.fetchall();
def closeConn(self):
try:
self.cursor.close();
self.connect.commit();
self.connect.close();
except EnvironmentError as err:
print('close connection failure:' + err);
def getConnection():
dbprov = 'SQLOLEDB' # ADO can use OLE
dbserv = '192.168.1.118,1433'
dbuser = 'sa'
dbpwd = 'pwd'
dbname = 'water'
return 'Provider=%s;Data Source=%s;Initial Catalog=%s;User ID=%s;Password=%s;' % (dbprov, dbserv, dbname, dbuser, dbpwd )
def updateRawData():
dbClass = DbHelper(getConnection());
try:
for row in dbClass.getExecResult("select * from rawdata"):
dbClass.execNoResult("update rawdata set value='{0}',proc_date=getdate() where pos='{1}'".format( "%.2f" % random.uniform(0,20),row[0]));
print("update success->" + dbClass.getDbTime());
dbClass.closeConn();
except EnvironmentError as err:
print('connection failure:' + err);
Timer(updateRawData,sleep=5).run()
Tested:
Python 3.3.0
Microsoft SQL Server 2008
'''
import time
import random
import adodbapi
import threading
'''
简单定时器类
'''
class Timer(threading.Thread):
def __init__(self,fn,args=(),sleep = 0):
threading.Thread.__init__(self)
self.fn = fn
self.args = args
self.interval = sleep
self.setDaemon(True)
self.enable = True
self.running = False
def __do(self):
self.running = True
self.fn(*self.args)
self.running = False
def run(self):
while self.enable :
time.sleep(self.interval)
self.__do()
def stop(self):
#stop the loop
self.enable = False
while True:
if not self.running : break
time.sleep(0.01)
'''
简单数据库访问类
'''
class DbHelper:
def __init__(self,conn):
self.conn = conn;
try:
self.connect = adodbapi.connect(conn,120)
self.cursor = self.connect.cursor();
except EnvironmentError as err:
print('connection failure:' + err);
def getConn(self):
return self.conn;
def getDbTime(self):
self.cursor.execute("select getdate()");
daterow = self.cursor.fetchone();
return str(daterow[0]);
def execNoResult(self,sql):
self.cursor.execute(sql);
self.connect.commit();
def getExecResult(self,sql):
self.cursor.execute(sql)
return self.cursor.fetchall();
def closeConn(self):
try:
self.cursor.close();
self.connect.commit();
self.connect.close();
except EnvironmentError as err:
print('close connection failure:' + err);
def getConnection():
dbprov = 'SQLOLEDB' # ADO can use OLE
dbserv = '192.168.1.118,1433'
dbuser = 'sa'
dbpwd = 'pwd'
dbname = 'water'
return 'Provider=%s;Data Source=%s;Initial Catalog=%s;User ID=%s;Password=%s;' % (dbprov, dbserv, dbname, dbuser, dbpwd )
def updateRawData():
dbClass = DbHelper(getConnection());
try:
for row in dbClass.getExecResult("select * from rawdata"):
dbClass.execNoResult("update rawdata set value='{0}',proc_date=getdate() where pos='{1}'".format( "%.2f" % random.uniform(0,20),row[0]));
print("update success->" + dbClass.getDbTime());
dbClass.closeConn();
except EnvironmentError as err:
print('connection failure:' + err);
Timer(updateRawData,sleep=5).run()