【发布时间】:2021-02-26 05:57:58
【问题描述】:
我正在编写一个可以向用户提问的机器人。我把答案放在我后来创建的类的对象中。在继续用户操作结束时,我会显示“发送答案”按钮。用户按下按钮,我将数据从对象传输到数据库。在那一刻我得到 type_error:
TypeError: object NoneType can't be used in 'await' expression
我检查我的对象 - 它不是空的。 我在这里添加我的代码:主文件(我将用户数据传输到数据库的地方)
@dp.callback_query_handler(lambda c: c.data == 'send')
def send_data_to_base():
if not db.user_exist(storage.user_id) == False:
db.add_new_user(storage.user_id, storage.gender, storage.age, storage.salary, storage.gov_rate,
storage.city_condition, storage.level_of_development)
数据库代码:
import sqlite3
class SQLighter:
def __init__(self, database):
self.connection = sqlite3.connect(database)
self.cursor = self.connection.cursor()
def get_info(self):
with self.connection:
return self.cursor.execute("SELECT * FROM `answers`").fetchall()
def user_exist(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT * FROM `answers` WHERE `user_id` = ?", (user_id,)).fetchall()
return bool(len(result))
def add_new_user(self, user_id, gender, age, salary, gov_rate, city_condition, level_of_development):
with self.connection:
return self.cursor.execute("INSERT INTO `answers` (`user_id`, `gender`, `age`, `salary`, `gov_rate`, `city_condition`, `level_of_development`) VALUES(?,?,?,?,?,?,?)", (user_id, gender, age, salary, gov_rate, city_condition, level_of_development))
PS:我正在使用 aiogram 库。
【问题讨论】:
标签: python sqlite bots telegram-bot