Forums

MYSQL error

from telethon import TelegramClient, events import re import mysql.connector

mydb = mysql.connector.connect(
  host="*******.mysql.pythonanywhere-services.com",
  user="******",
  password="********",
  database="*******$message"
)

mycursor = mydb.cursor()

api_id = **********
api_hash = '************'
client = TelegramClient('********', api_id, api_hash)

@client.on(events.NewMessage(forwards=False))
async def handler(event):
    if event.is_reply:
        r_chat_id = event.chat_id
        r_chat_reply_id = event.reply_to_msg_id
        chat = await event.get_chat()
        query = ("SELECT forward_id FROM Message "
                  "WHERE channel_id = %s AND source_id = %s")
        mycursor.execute(query, (r_chat_id, r_chat_reply_id))
        myresult = mycursor.fetchone()
        if(myresult):
            for row in myresult:
                await client.send_message(-*************, event.text +'\n\n -- ' + chat.title, reply_to=row)
        else:
            print('Not Found orginal message')
    chat = await event.get_chat()
    chat_id = event.chat_id
    msg = event.raw_text
    numbers = re.findall('\d+\.\d+|\d+',msg)
    if len(numbers) >= 3:
        if re.findall('(?:buy|sell)(?=.*old)(?=.*new)', msg, flags=re.IGNORECASE | re.DOTALL):
            sent = await client.send_message(-************, event.text +'\n\n -- ' + chat.title)
            sql = "INSERT INTO Message (channel_id, source_id, forward_id) VALUES (%s, %s, %s)"
            val = (chat_id, event.id, sent.id)
            mycursor.execute(sql, val)
            mydb.commit()

        else:
            print('Not match')


client.start()
client.run_until_disconnected()

When I run this code getting MYSQL error how to solve this here is the error code example

Unhandled exception on handler
Traceback (most recent call last):
  File "/home/muben/.local/lib/python3.9/site-packages/telethon/client/updates.py", line 454, in _dispatch_update
    await callback(event)
  File "/home/muben/site/telegram.py", line 42, in handler
    mycursor.execute(sql, val)
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection.py", line 651, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection.py", line 375, in _send_cmd
    return self._socket.recv()
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/network.py", line 267, in recv_plain
    raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

Traceback (most recent call last):
  File "/home/muben/.local/lib/python3.9/site-packages/telethon/client/updates.py", line 454, in _dispatch_update
    await callback(event)
  File "/home/muben/site/telegram.py", line 26, in handler
    mycursor.execute(query, (r_chat_id, r_chat_reply_id))
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection.py", line 651, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection.py", line 367, in _send_cmd
    self._socket.send(
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/network.py", line 166, in send_plain
    raise errors.OperationalError(
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'muben.mysql.pythonanywhere-services.com:3306', system error: 32 Broken pipe

It looks like your idle connection is not closed and times out.

I'm a newbie can you please send me the code

We can't write your code for you, but the easiest way forward would be to close the DB connection at the end of handling each message, and to open a new one every time you need it. In the longer term, I'd recommend you look into using a database connection manager like SQLAlchemy.