Я написал простую программу связанную с базой данных MySQL и ВК, "регистрация пользователя".
Вот код:
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})
После написания боту "register", Выдаёт две ошибки:
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'