I
I
Iangyl2020-11-15 23:15:23
Python
Iangyl, 2020-11-15 23:15:23

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)

data base
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))


The object was checked for emptiness - it is not empty.

PS: I use the aiogram library.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-11-16
@Iangyl

Shouldn't the send_data_to_base function be asynchronous?
async def send_data_to_base():

S
Sergey Karbivnichy, 2020-11-15
@hottabxp

Use MySQL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question