Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ def _quote_name(name):
return '"{0}"'.format(name.replace('"', '""'))


def _escape_single_quotes(value):
return value.replace("'", "''")


def _quote_value(value):
return "'{0}'".format(value.replace("'", "''"))
return "'{0}'".format(_escape_single_quotes(value))


def _iterdump(connection, *, filter=None):
Expand Down Expand Up @@ -80,11 +84,12 @@ def _iterdump(connection, *, filter=None):
table_name_ident = _quote_name(table_name)
res = cu.execute(f'PRAGMA table_info({table_name_ident})')
column_names = [str(table_info[1]) for table_info in res.fetchall()]
q = "SELECT 'INSERT INTO {0} VALUES('{1}')' FROM {0};".format(
table_name_ident,
q = "SELECT 'INSERT INTO {0} VALUES('{1}')' FROM {2};".format(
_escape_single_quotes(table_name_ident),
"','".join(
"||quote({0})||".format(_quote_name(col)) for col in column_names
)
),
table_name_ident,
)
query_res = cu.execute(q)
for row in query_res:
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ def test_table_dump(self):
[self.assertEqual(expected_sqls[i], actual_sqls[i])
for i in range(len(expected_sqls))]

def test_dump_single_quote_in_identifier(self):
# A single quote in a table or column name must not break the dump.
self.cu.execute("""CREATE TABLE "a'b" ("c'd" text);""")
self.cu.execute("""INSERT INTO "a'b" VALUES('x''y');""")
expected = [
"BEGIN TRANSACTION;",
"""CREATE TABLE "a'b" ("c'd" text);""",
"""INSERT INTO "a'b" VALUES('x''y');""",
"COMMIT;",
]
actual = list(self.cx.iterdump())
self.assertEqual(expected, actual)
# The dump restores into a fresh database.
with memory_database() as cx2:
cx2.executescript("".join(actual))
row = cx2.execute("""SELECT "c'd" FROM "a'b";""").fetchone()
self.assertEqual(row[0], "x'y")

def test_table_dump_filter(self):
all_table_sqls = [
"""CREATE TABLE "some_table_2" ("id_1" INTEGER);""",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`sqlite3.Connection.iterdump` raising :exc:`sqlite3.OperationalError`
when a table name contains a single quote. Patch by tonghuaroot.
Loading