mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-05-03 16:40:19 +08:00
34 lines
963 B
Python
34 lines
963 B
Python
class Insert:
|
|
def __init__(self, connector, table_name: str, data: dict):
|
|
self._connector = connector
|
|
self._table_name = table_name
|
|
self._columns = data.items()
|
|
|
|
def __gen_id(self) -> int:
|
|
self._connector.execute(
|
|
f"""
|
|
SELECT MAX(id) FROM {self._table_name}
|
|
""",
|
|
)
|
|
max_id = self._connector.fetchone(keys=["id"]).get("id")
|
|
if max_id is None:
|
|
return 1
|
|
return max_id + 1
|
|
|
|
def one(self, data: dict):
|
|
_id = self.__gen_id()
|
|
data["id"] = _id
|
|
columns = ", ".join(data.keys())
|
|
placeholders = ", ".join([f":{key}" for key in data.keys()])
|
|
self._connector.execute(
|
|
f"""
|
|
INSERT INTO {self._table_name} ({columns})
|
|
VALUES ({placeholders})
|
|
""",
|
|
data,
|
|
)
|
|
|
|
def many(self, data: list[dict]):
|
|
for item in data:
|
|
self.one(item)
|