1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import random import time import threading from connect_DB.connect_sqlserver import * class MyThread(threading.Thread): def __init__(self, min, max, table_name, sleep_time): threading.Thread.__init__(self) self.conn = ConnectSqlserver(host='localhost', user="username", password="password", db="databasename") self.sleep_time = sleep_time self.table_name = table_name self.min = min self.max = max def run(self): while True: time.sleep(self.sleep_time) data = random.randint(self.min, self.max) sql = 'INSERT INTO ' + self.table_name + '([VALUE]) VALUES(' + str(data) + ');' self.conn.run_sql(sql) def main(): try: thread1 = MyThread(100, 120, 'DATA1', 1) thread2 = MyThread(100, 120, 'DATA2', 1) thread1.start() thread2.start() except Exception as e: print('Error: ', e) while 1: pass if __name__ == '__main__': main()
|