Files
cksd/models/ammeter_usage.py

56 lines
1.7 KiB
Python

import time
import aiosqlite
from database import get_conn
class AmmeterUsageModel:
@classmethod
async def create_table(cls):
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS ammeter_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id INTEGER,
room_name TEXT,
left_ele REAL,
left_money REAL,
left_free_ele REAL,
left_free_money REAL,
ele_price REAL,
mon_time INTEGER,
created_at INTEGER UNIQUE
)
"""
)
await conn.commit()
@classmethod
async def insert(cls, data: dict):
print(data)
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
await conn.execute(
"""
INSERT OR IGNORE INTO ammeter_usage (
room_id, room_name,
left_ele, left_money,
left_free_ele, left_free_money,
ele_price, mon_time, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
data["roomId"],
data["roomName"],
data["leftEle"],
data["leftMoney"],
data["leftFreeEle"],
data["leftFreeMoney"],
data["elePrice"],
data["monTime"],
int(time.time() * 1000),
),
)
await conn.commit()