我正在制作一个日程通知机器人,我必须接触它们классы
,但我从未遇到过它们:
import sqlite3
class RequestsInBD:
def __init__(self):
self.connection = sqlite3.Connection('datebase_of_users.db')
self.cursor = self.connection.cursor()
def _commit_and_close_bd(self, connection):
connection.commit()
connection.close()
def check(self, user_id):
all_id = self.cursor.execute('SELECT id FROM grade_of_users').fetchall()
self._commit_and_close_bd(self.connection)
for item in all_id:
if user_id in item:
return False
return True
def add_user(self, user_id, grade):
self.cursor.execute(f'INSERT INTO grade_of_users (id, grade, notifications) VALUES (?, ?, ?)', (user_id, grade, 1))
self._commit_and_close_bd(self.connection)
def return_user_grade(self, user_id):
self.cursor.execute(f'SELECT id, grade FROM grade_of_users WHERE id = {user_id}')
grade = self.cursor.fetchone()[1]
self._commit_and_close_bd(self.connection)
return grade if grade else False
def get_users(self):
self.cursor.execute('SELECT id, grade, notifications FROM grade_of_users')
res = self.cursor.fetchall()
self._commit_and_close_bd(self.connection)
return res
def new_grade(self, grade, user_id):
self.cursor.execute(f'UPDATE grade_of_users SET grade = ? WHERE id = ?', (grade, user_id))
self._commit_and_close_bd(self.connection)
代码看起来可以工作,但是写得正确吗? Chatgpt 说原则上一切都很好,但我有点不确定他的话。 (此代码出现在与主程序分开的文件中,我从主程序导入此类,并且每次调用时我都会创建此类的对象(实例),我不确定这是否也是正确的)