title: How to restore sqlite3 database file programmatically in django
body: <p>The user wishes to have a database backup and restore functionality through the application. They want to be able to download the database file and upload it to restore the database whenever needed. The problem is that django is already running the current DB file. I wrote the following logic to restore the database.</p>
  <pre><code>    folder ='./' 
  if request.method == 'POST':        
      myfile = request.FILES['file']
      fs = FileSystemStorage(location=folder) 
      if myfile.name.split(&quot;.&quot;)[1] != &quot;sqlite3&quot;:
          return JsonResponse({&quot;res&quot;:&quot;Please upload a database file.&quot;})
      if os.path.isfile(os.path.join(folder, &quot;db.sqlite3&quot;)): 
          os.remove(os.path.join(folder, &quot;db.sqlite3&quot;))        
      filename = fs.save(&quot;db.sqlite3&quot;, myfile)
      file_url = fs.url(filename)
      return JsonResponse({&quot;res&quot;:file_url})
  </code></pre>
  <p>I get the following error which is rightly justified:</p>
  <blockquote>
  <p>[WinError 32] The process cannot access the file because it is being used by another process</p>
  </blockquote>
  <p>So, is there a way I can achieve this functionality through my application?</p>