I
I
Izzy Kotov2021-07-03 03:09:20
Python
Izzy Kotov, 2021-07-03 03:09:20

How to fix sqlite3.IntegrityError: NOT NULL constraint failed?

from RXConfigBase import bot, logger
from RXSQLConfig import sqlite3, user_id, name, surname, patronymic


#Начало, логиниться и регистрация
@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.from_user.id,  'Добро пожаловать!\n' f'‍♂️>> {message.chat.first_name}|{message.chat.last_name} <<‍♀️\n\n' 'Входа в систему /log\n' 'Регистрация /reg')

#Регистрация
@bot.message_handler(commands=['reg'])
def start_message(message):
    bot.send_message(message.from_user.id, 'Регистрацию аккаунта.\nНачнём?(Напиши "next")')
    bot.register_next_step_handler(message, name)

#Собираем данные для регистрации и БД
def name(message): #получаем имя
    bot.send_message(message.from_user.id, 'Укажите имя')
    global name
    global user_id
    user_id = message.from_user.id
    name = message.text
    conn = sqlite3.connect("DataBase/RXDataBase.db")
    cursor = conn.cursor()
    cursor.execute("INSERT INTO Profile (user_id, name) VALUES (?, ?)", (user_id, name, ))
    #cursor.execute(f"SELECT get_user_id FROM Profile WHERE id = {message.from_user.id}")
    #cursor.execute(f"SELECT get_name FROM Profile WHERE id = {message.text}")
    bot.register_next_step_handler(message, surname)

def surname(message): #получаем фамилии
    bot.send_message(message.from_user.id, 'Укажите фамилию')
    global surname
    surname = message.text
    conn = sqlite3.connect("DataBase/RXDataBase.db")
    cursor = conn.cursor()
    cursor.execute("INSERT INTO Profile (surname) VALUES (?)", (surname, ))
    bot.register_next_step_handler(message, patronymic)

def patronymic(message): #получаем отчества
    bot.send_message(message.from_user.id, 'Укажите отчество')
    global patronymic
    patronymic = message.text
    conn = sqlite3.connect("DataBase/RXDataBase.db")
    cursor = conn.cursor()
    cursor.execute("INSERT INTO Profile (patronymic) VALUES (?)", (patronymic, ))
    return




if __name__ == '__main__':
    bot.polling(none_stop=True, interval=0)


Error:
cursor.execute("INSERT INTO Profile (user_id, name) VALUES (?, ?)", (user_id, name))
sqlite3.IntegrityError: NOT NULL constraint failed: Profile.surname

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2021-07-03
@HellcatT150

sqlite3.IntegrityError: NOT NULL constraint failed: Profile.surname

You are trying to insert an empty field into the database.
Solution options:
  • Do not insert rows with empty fields for which the NOT NULL constraint is set
  • Remove NOT NULL constraint

UPD : At first I did not look at the code, and then I looked at it.
You are trying to insert data, but you are inserting separate fields in different functions.
You should first ask the user for the entire set of necessary data, store them in variables, and only then insert them into the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question