0

I am having some troubles moving one *.sqlite ddbb that I've created from one folder to another.

this is my attempt:

Import os 
Import sqlite3
Import pandas as pd

#There is some code here to get my pandas dataframe

Then the goal of the following code is: To create a sqlite ddbb from a pandas dataframe (dfGlobalProcess) and then move it to another subfolder named "Projects_ddbb".If I could save it directly to this another folder it would be great.

This is the code:

ddbbName='ExampleProject1' #I know this line does nothing right now... but in the future this should be an user input     
ddbbName2= ddbbName + '.sqlite'

conn = sqlite3.connect('' + ddbbName2 +'')     
dfGlobalProcess.to_sql('ProcessesElemFlows', conn, index=False, if_exists='replace') 
conn.close() 

prevName='' + ddbbName2 + ''
newName='Projects_ddbb/' + prevName + ''

os.rename(prevName,newName)

When I run this code I get the following error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'ExampleProject1.sqlite' -> 'Projects_ddbb/ExampleProject1.sqlite'

I've searched for solutions here in stackoverflow but I am not able to figure out how to solve it

if I run the same code but moving a *.txt file that is in the same folder in which the ddbb is stored, the *.txt file is moved to the 'Projects_ddbb' folder as expected.

It looks like I have the ddbb opened in some way, but as I am fairly new to Python and programming in general I don't have any idea how to proceed.

I've also tried something like this, but I don't know if it makes any sense... probably not because it does not work:

os.close('ExampleProject1.sqlite')

Any help will be much appreciated.

  • Does it work if you replace the entire `to_sql` call with `conn.execute('CREATE TABLE test(test)')`? – CL. May 06 '18 at 13:24
  • It may be just a timing issue. Windows is very quick to place a hold on a file in use, but notoriously slow at releasing that hold. – John Anderson May 06 '18 at 13:25
  • Thank you very much for the answers. I figured out how to save the *.sqlite table directly in the desired folder using this code line: ddbbName2= os.path.join(CurrentDirectory, 'Projects_ddbb\\'+ddbbName + '.sqlite') and works perfectly... maybe as John Anderson said is just a timing issue, in any case, thank you very much for your answers – joan colon May 06 '18 at 13:26

0 Answers0