G
G
Guerro692020-05-15 21:18:39
MySQL
Guerro69, 2020-05-15 21:18:39

AttributeError: 'NoneType' object has no attribute 'close'?

I wrote a simple program related to MySQL database and VC, "user registration".
Here is the code:

import pymysql.cursors
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from plugins import system
import random
import time
import re
from datetime import datetime, timedelta

def connection():
  connection = pymysql.connect(host='localhost', user='root', password='root', db='testbase', cursorclass=pymysql.cursors.DictCursor)

def register(user_id):
  connect = connection()
  try:
    with connect.cursor() as cursor:
      result = cursor.execute(f'SELECT * FROM accounts WHERE uid={user_id}')
      if result == 0:
        cursor.execute(f'INSERT INTO accounts(uid) VALUES({user_id})')
        connect.commit()
        return 'Вы успешно зарегистрировались'

      else:
        return 'Вы уже зарегистрированны'

  finally:
    connect.close()

rand = random.randint(-2147483648, 2147483648)

vk_session, vk, longpoll = system.session()

print('logged')

while True:
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW:
            message = event.obj.text
            if message == 'register':
                text = function.register(event.obj.from_id)
                vk.messages.send(peer_id=event.obj.peer_id, random_id=rand, message={text})

After writing "register" to the bot, Throws two errors:

with connect.cursor() as cursor:
AttributeError: 'NoneType' object has no attribute 'cursor'

During handling of the above exception, another exception occurred:

connect.close()
AttributeError: 'NoneType' object has no attribute 'close'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-05-15
@Guerro69

In procedure you return nothing It is nothing you assign connect and try to pull its nonexistent methods. Here is the error and gives you
def connection():

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question