My python 35 script needs at some point to run retrieve some data from .sqlite file (let's call it src.sqlite
). It doesn't need to write just to read. But it can't access directly src.sqlite
with sqlite3 because src.sqlite
is locked by another program (sqlite3.OperationalError: database is locked
)
So I made a copy of src.sqlite
(copy.sqlite
) with shutil.copy2
and I run my Select SQL query on copy.sqlite
. After having retreived the data I need, I want to delete copy.sqlite
and thus run shutil.rmtree()
, but I get this error:
[WinError 32] The process cannot access the file because it is being used by another process.
When looking for a solution for this error I came accross the possibility of using temp file (yes, I am new to Python!). So I am now trying to create a temp file in which I will copy src.sqlite
and then I should run my SQL query on this temp.sqlite
. I have seen this question and that one but I have trouble adapting them to my needs. So far this is what I have done:
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, 'temp_file_name')
shutil.copy2("C:\\src.sqlite", temp_path)
print('exists', os.path.isfile(temp_path))
print(temp_dir)
But when I run my SQL query on that temp I get this error sqlite3.OperationalError: no such table XXX
.