Answer the question
In order to leave comments, you need to log in
How to transfer data from the bot to the database?
I am writing a bot-questionnaire in python for TG. I put the data in the object that I created earlier.
I also created a database using SQLighter. At the end of the poll, I upload the data to the database, and I get an error:
TypeError: object NoneType can't be used in 'await' expression
I'll attach
the code here:
bot
@dp.callback_query_handler(lambda c: c.data == 'send')
def send_data_to_base():
if not db.user_exist(storage.user_id):
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))
Answer the question
In order to leave comments, you need to log in
Shouldn't the send_data_to_base function be asynchronous?
async def send_data_to_base():
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question