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 get this message when I try to connect to start or connect the client:
And finally, I get OperationalError: database is locked
error when I try to log in using my phone.
The error message in full:
--------------------------------------------------------------------
OperationalError Traceback (most recent
call last)
<ipython-input-13-880bc0e4ea12> in <module>()
1 from telethon import TelegramClient, sync
----> 2 client = TelegramClient('session_name', api_id, api_hash)
3
4 client.connect()
5 if not client.is_user_authorized():
/anaconda3/lib/python3.7/site-
packages/telethon/client/telegrambaseclient.py in __init__(self,
session, api_id, api_hash, connection, use_ipv6, proxy, timeout,
request_retries, connection_retries, retry_delay, auto_reconnect,
sequential_updates, flood_sleep_threshold, device_model,
system_version, app_version, lang_code, system_lang_code, loop,
base_logger)
221 DEFAULT_DC_ID,
222 DEFAULT_IPV6_IP if self._use_ipv6 else
DEFAULT_IPV4_IP,
--> 223 DEFAULT_PORT
224 )
225
/anaconda3/lib/python3.7/site-packages/telethon/sessions/sqlite.py
in set_dc(self, dc_id, server_address, port)
184 def set_dc(self, dc_id, server_address, port):
185 super().set_dc(dc_id, server_address, port)
--> 186 self._update_session_table()
187
188 # Fetch the auth_key corresponding to this data center
/anaconda3/lib/python3.7/site-packages/telethon/sessions/sqlite.py
in _update_session_table(self)
205 # some more work before being able to save auth_key's
for
206 # multiple DCs. Probably done differently.
--> 207 c.execute('delete from sessions')
208 c.execute('insert or replace into sessions values
(?,?,?,?)', (
209 self._dc_id,
OperationalError: database is locked
What does coroutine object AuthMethods._start at mean?
Why is it giving database is locked?
6
Answers
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
TelegramClient started
Second
TelegramClient’s already active
This also causes the
database is locked
error. More info: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.
Hope this helps.
To solve the infamous
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:assuming you are using the
SESSION_NAME
string variable to store the session name parameter of theTelegramClient()
function.If you’re
client.start()
in'if __name__ == '__main__':'
, you cannot log in to the telegram app and you will haveyou can check the active processes for the session and if there are any, then close them before starting the client
If you just need to get rid of this issue in Linux and run the program again, follow the below steps.
Step 1 – Find the session file. You can find a file called xxxxxxxxx.session In the same directory where you have your python script saved.
Step 2 – run
fuser xxxxxxxxx.session
(replace xxxxxxxxx.session with the file name found in step 1 )Step 3 – You will see a response with an integer (Ex – 590219)
Step 4 – Run
kill -9 590219
(replace 590219 with the integer found in the step 3)Now run your program again