Spaces:
Sleeping
Sleeping
File size: 824 Bytes
9d5ce65 | 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 | import sqlite3
from datetime import datetime
conn = sqlite3.connect("traffic.db", check_same_thread=False)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS traffic_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT,
cars INTEGER,
bikes INTEGER,
bus INTEGER,
truck INTEGER,
no_helmet INTEGER
)
""")
def insert_data(data):
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute("""
INSERT INTO traffic_data (date, cars, bikes, bus, truck, no_helmet)
VALUES (?, ?, ?, ?, ?, ?)
""", (date, data["cars"], data["bikes"], data["bus"], data["truck"], data["no_helmet"]))
conn.commit()
def get_data():
cursor.execute("SELECT * FROM traffic_data ORDER BY id DESC LIMIT 50")
return cursor.fetchall()
|