2

I'm trying to rename a geopackage table from a QGIS 3.8.0 Python processing script.

But it leaves a broken dataset in 2 parts, where the tabular data table has been renamed but leaves the geometry table not renamed.

enter image description here

Here's the code:

import sqlite3

geopPath = r"C:\GIS\Processing\Tool Processing\NaturalEarth2.gpkg

tab = 'ne_10m_admin_0_countries'

newTab = 'nev_10m_admin_0_countries'

con = sqlite3.connect(geopPath)

cursor = con.cursor()

query01 = "ALTER TABLE '{}' RENAME TO '{}';".format(tab,newTab)

query02 = "UPDATE {} SET table_name = '{}' WHERE table_name = '{}';".format('gpkg_ogr_contents',newTab,tab)

query03 = "UPDATE {} SET table_name = '{}' WHERE table_name = '{}';".format('gpkg_geometry_columns',newTab,tab)

cursor.execute(query01)

cursor.execute(query02)

cursor.execute(query03)

con.commit()

cursor.close()

con.close() 

That seems to commit without error. And I suspect query 01 and 02 are doing the same thing.

I don't think editing the gpkg_geometry_columns is needed as it's only a tablename change, the CRS remains the same.

This link is quite useful but can't find the part that helps me.

Which columns in which tables in the underlying geopackage structure do I need to edit?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.