0

I try to copy a GPKG to another folder. The result is the correct named copied GPKG but there is only one layer in the copy. It’s the layer with the same name as the GPKG. The other features (1x polygon, 1x no geometry) from the original are not copied. The original GPKG is open with QGIS when I start the copy-process. Is there another simple way to copy the whole GPKG or do I have to create a new GPKG and copy the layers of the original GPKG to it?

from shutil import copy
inputgpkg = r'C:\Testfolder\original.gpkg'
outputgpkg = r'C:\Testfolder2\copy.gpkg'
copy(inputgpkg,outputgpkg)
6
  • Why not to copy the gpkg file with file system tools?
    – user30184
    Jan 5 at 13:28
  • what do you mean by 'file system tools'? Doing it manually? It's part of a script and I don't want to forget this step when executing the script.
    – Motti
    Jan 5 at 13:35
  • 1
    Sorry, for some reason I thought that shutil has a connection to shapefiles but of course it works with files without knowing anything about the contents. I tried your Python commands and copy worked perfectly. I can only imagine that you have done some editing with QGIS and changes were not yet added into the main database file .gpkg but appeared only in the write ahead log files (.wal and .shm) sqlite.org/wal.html which you did not copy. Either copy all three files or close the connection from QGIS first, that guarantees that the changes are committed and .wal and .shm disappear.
    – user30184
    Jan 5 at 14:00
  • I tried to copy the (-wal and –shm) as well. This results in the GPKG not containing any layer-warning. I think, the problem is, that I have the GPKG opened in QGIS while running the code. I don’t know if it is possible and save to quit the connection to a GPKG while having it open and reconnect again. So I may have to use plan b and create a new GPKG, then copy all the layers separately.
    – Motti
    Jan 5 at 16:09
  • Gpkg+wal+shp should contain all the data and when the main database file is opened the SQLite engine should make all the queued edits from .wal and .shm automatically, and finally remove .wal and .shm.
    – user30184
    Jan 5 at 16:56
2

As user30184 stated, one needs to copy the -wal and -shm file as well:

from shutil import copy
inputgpkg = r'C:\Testfolder\original.gpkg'
outputgpkg = r'C:\Testfolder2\copy.gpkg'
copy(inputgpkg,outputgpkg)
copy(inputgpkg+'-wal',outputgpkg+'-wal')
copy(inputgpkg+'-shm',outputgpkg+'-shm')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.