Skip to content

Instantly share code, notes, and snippets.

@Nowosad
Created July 25, 2025 09:03
Show Gist options
  • Select an option

  • Save Nowosad/357f5ab94b39809c93e2321a8c10c066 to your computer and use it in GitHub Desktop.

Select an option

Save Nowosad/357f5ab94b39809c93e2321a8c10c066 to your computer and use it in GitHub Desktop.
library(sf)
library(dplyr)
library(spData)
library(DBI)
library(RSQLite)
# save nz to a file
write_sf(nz, "my_nz.gpkg", layer = "nz")
# connect to the GeoPackage
conn = dbConnect(RSQLite::SQLite(), "my_nz.gpkg")
# check tables
dbListTables(conn)
# disable triggers that work on geometries
triggers = dbGetQuery(conn, "SELECT name FROM sqlite_master WHERE type = 'trigger';")
for (trigger_name in triggers$name) {
dbExecute(conn, paste0("DROP TRIGGER IF EXISTS ", trigger_name, ";"))
}
# check the table structure
dbGetQuery(conn, "PRAGMA table_info(nz);")
# update second row
dbExecute(conn, "UPDATE nz SET Name = 'New Region Name' WHERE fid = 1;")
# confirm update
dbGetQuery(conn, "SELECT fid, Name FROM nz WHERE fid = 1;")
# disconnect
dbDisconnect(conn)
# check in R
nz2 = read_sf("my_nz.gpkg", layer = "nz")
nz2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment