-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.py
More file actions
43 lines (36 loc) · 1006 Bytes
/
Copy pathwrite.py
File metadata and controls
43 lines (36 loc) · 1006 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
import time
import mysql.connector
import json
import random
from datetime import datetime
def db_connection():
mydb = mysql.connector.connect( host = 'comp123.cafkc5h3ic4r.us-east-1.rds.amazonaws.com',
user = 'admin',
port = '3306',
database = 'comp123',
passwd = 'password',
autocommit = True)
#print("successfully connect to the database")
return mydb
mydb = db_connection()
cur = mydb.cursor()
def genData():
driverID = "duxu1000009"
speed = random.randint(0,140)
now = datetime.now()
timeNow = now.strftime('%Y-%m-%d %H:%M:%S')
data = {}
data['driverID'] = driverID
data['speed'] = speed
data['timeNow'] = timeNow
return data
def execute():
data = genData()
print(data)
sql = "insert into Monitor(driverID, speed, time) values ('{0}',{1},'{2}')".format(data['driverID'], data['speed'], data['timeNow'])
print(sql)
ret = cur.execute(sql)
print("insert one record")
while True:
execute()
time.sleep(1)