Skip to content Skip to sidebar Skip to footer

Telethon: Operationalerror: Database Is Locked

Apologies if it is a silly question. I am trying telethon for the first time and it fails to synchronize with my telegram API. I get an IP address when I type this code: But I ge

Solution 1:

Referring to the Telethon documentation, The database will be locked if you don't close it properly.

In your case, you are using the same session file from many TelegramClient's at once.

First

In [9] client.start()

TelegramClient started

Second

In [13] client.connect()

TelegramClient's already active

This also causes the database is locked error. More info:

Solution 2:

You have 2 problems here, first problem is related to authentication i guess, i will talk about database lock problem :

Session name that you have passed is already in use or active hence locked.

It becomes session file name if you use string as a parameter like here you have passed "name", this is one way to create a session.

other wise you can use telethon.sessions.abstract.Session object and pass it as parameter , this is second way.

And third way, you can simply pass None.

If it’s None, the session will not be saved, and you should call log_out() when you’re done.

client =  TelegramClient(None, api_id, api_hash)

Hope this helps.

Solution 3:

To solve the infamous

OperationalError: database is locked

error caused by sqllite3, one solution is to find the <SESSION_NAME>.session file created the first time you ran the code and delete it. Telethon puts those files in the same folder as your Python code or Jupyter Notebook. Or alternatively, you could use Python to get rid of the file automatically:

import os
import sys
os.chdir(sys.path[0])

iff"{SESSION_NAME}.session"in os.listdir():
    os.remove(f"{SESSION_NAME}.session")

assuming you are using the SESSION_NAME string variable to store the session name parameter of the TelegramClient() function.

Solution 4:

If you're client.start() in 'if __name__ == '__main__':', you cannot log in to the telegram app and you will have

'OperationalError: database is locked'. You must delete 'if name == 'main'' before client.start()

Solution 5:

you can check the active processes for the session and if there are any, then close them before starting the client

pid = check_output(['fuser', 'anon.session'])
 if pid:
    check_output(['kill', pid])

Post a Comment for "Telethon: Operationalerror: Database Is Locked"