[英]How to make (correctly) runtime database connection in python telegram bot?
我正在使用 python+peewee+postgresql(没有 django)做我的第一个电报机器人项目。
我只想知道,如何连接到我的数据库不是一次(在我的项目代码开始时),而是每次需要时。 例如:用户点击按钮 -> 连接打开 -> 将行添加到表中
现在文件结构如下所示:
tgbot.py
# some lines with imports
updater = Updater(TELEGRAM_TOKEN)
dp = updater.dispatcher
dp = setup_dispatcher(dp)
# some lines with logging config
updater.start_polling()
updater.idle()
调度程序.py
# some lines with imports
# some def for event handlers
# !!!!! here will be database connections (in functions for events)
def setup_dispatcher(dp):
db_conn = DBConnection() # Enter database connection
from dbmodels import db
db = db_conn.get_connection()
ToDo.create(...) # creating line in postgres-table
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('location', ask_for_location))
dp.add_handler(MessageHandler(Filters.location, change_location))
dp.add_handler(CommandHandler('today', ask_for_today))
return dp
dbhelper.py
# some lines with imports
class DBConnection(Singleton):
def __init__(self): ... # initializing some variables like self.database, etc
def get_connection(self):
""" Creating PostgreSQL's database connection """
if self.connection is None:
try:
self.connection = PostgresqlDatabase(self.database, user=self.user, password=self.password, host=self.host, port=self.port)
self.curs = self.connection.cursor()
except (Exception, Error) as error:
print("PostgreSQL's connection error: \n", error)
sys.exit(1)
return self.connection
def __del__(self):
""" Closing database connection """
if self.connection is not None:
self.curs.close()
self.connection.close()
print("PostgreSQL's connection closed.")
数据库模型.py
# some lines with imports
db = PostgresqlDatabase(None)
class Singleton:
""" Singleton realisation for database connection class in dbhelper.py """
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
class BaseModel(Model):
""" Basic class, used for tables-classes in models.py """
class Meta:
database = db
模型.py
# some lines with imports
class User(BaseModel):
""" A model for client's table """
# initializing some fields
class Meta:
db_table = "clients"
...
class ToDo(BaseModel):
""" A model of to-do's table """
# initializing some fields
class Meta:
db_table = "to_do_s"
...
这都是完全没有必要的。 peewee 数据库对象已经是一个单例。 当你想连接/关闭时,你只需要调用它的connect()
和close()
方法。
或者,您可以将数据库实例用作上下文管理器,例如:
db = PostgresqlDatabase(...)
with db:
ToDo.create(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.