def copy_table(conn_in, conn_out, table_name, constraint=None):
cursor_in = conn_in.cursor()
#Check that table exists
cursor_in.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='%s';"
% table_name
)
if cursor_in.fetchone():
cursor_in.execute(
"SELECT * FROM \"%s\"%s;" % (table_name, "" if constraint is None
else " WHERE " + constraint)
)
with conn_out.cursor() as cursor_out:
for record in cursor_in:
values = record_to_string(record)
try:
cursor_out.execute(
"INSERT INTO \"%s\" VALUES (%s);" %
(table_name, values)
)
except psycopg2.IntegrityError as e:
conn_out.rollback()
if e.pgcode == '23505':
sys.stderr.write(
"ERROR: GeoPackage seems to be already imported. "
"Error message was: '%s'.\n" % e.message
)
sys.exit(1)
except Exception as e:
conn_out.rollback()
sys.stderr.write(
"ERROR: Input doesn't seem to be a valid GeoPackage. "
"Error message was: '%s'.\n" % e.message
)
sys.exit(1)
评论列表
文章目录