E
E
EVG822022-03-05 19:06:09
Python
EVG82, 2022-03-05 19:06:09

Why is name 'sqlite3' is not defined error?

db.py file created

import sqlite3
from sqlite3 import Error
import os

conn = sqlite3.connect('baza1.db')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS baza1(user_id INTEGER, username TEXT, mail TEXT)')
conn.commit()


in main
import db,

when I start the bot, I get the error
name 'sqlite3' is not defined

, yet the code for execution:

@dp.message_handler(content_types=['text'])
async def get_message(message):
   if message.text == "Сценарий1":  # нажав сценарий мы должны получить поле для ввода емейл
       await message.answer("Введите электронную почту. Она требуется для корректной работы с вами далее", reply_markup=ReplyKeyboardRemove())
       await NewPost.next()
@dp.message_handler(state=NewPost.email)
async def isValid(message: types.Message, state: FSMContext):
           email = message.text

           await state.update_data(
               {'email': email}
           )

           regex = re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9][email protected][A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')

           if re.fullmatch(regex, email):
               print("Valid email")

           #await message.reply('Привет, я эхо-бот, напиши мне что-нибудь!')
           try:
                   conn = sqlite3.connect('baza1.db')
                   cur = conn.cursor()
                   cur.execute('CREATE TABLE IF NOT EXISTS baza1(user_id INTEGER, username TEXT, mail TEXT)')
                   conn.commit()
           except Exception as e:
                   print(e)
                   conn = sqlite3.connect('baza1.db')
                   cur = conn.cursor()
                   cur.execute('CREATE TABLE IF NOT EXISTS baza1(user_id INTEGER, username TEXT, mail TEXT)')
                   conn.commit()


Error in console:
File "G:\PYTHON\pythonProject123\main.py", line 103, in isValid
    conn = sqlite3.connect('baza1.db')
NameError: name 'sqlite3' is not defined
Valid email
name 'sqlite3' is not defined


They write that in Python itself there may be a jamb in the module.
Like it needs to be rebuilt somehow ...

and here the question

is the PANDAS module,

can it be better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-03-05
@EVG82

1. Imports don't work that way. All modules must be included directly in each file in which it is called (plus explicit is better than implicit ) - i.e. if the sqlite module is needed in main.py , then you need to import it in main.py , regardless of whether it is included in db.py or not.
2. Which is better, a fork or a mug? (In other words, sqlite and pandas do different things)
----
3. What is the point of db.py if the code from it is duplicated in main?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question