Skip to content

Commit 9bf6b06

Browse files
committed
gh-153658: Fix sqlite3 iterdump() for table names containing a single quote
Patch by tonghuaroot.
1 parent ed71655 commit 9bf6b06

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lib/sqlite3/dump.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@ def _iterdump(connection, *, filter=None):
8080
table_name_ident = _quote_name(table_name)
8181
res = cu.execute(f'PRAGMA table_info({table_name_ident})')
8282
column_names = [str(table_info[1]) for table_info in res.fetchall()]
83-
q = "SELECT 'INSERT INTO {0} VALUES('{1}')' FROM {0};".format(
84-
table_name_ident,
83+
# In the string-literal copy any single quote must be doubled;
84+
# the FROM clause below uses the identifier form as-is.
85+
table_name_literal = table_name_ident.replace("'", "''")
86+
q = "SELECT 'INSERT INTO {0} VALUES('{1}')' FROM {2};".format(
87+
table_name_literal,
8588
"','".join(
8689
"||quote({0})||".format(_quote_name(col)) for col in column_names
87-
)
90+
),
91+
table_name_ident,
8892
)
8993
query_res = cu.execute(q)
9094
for row in query_res:

Lib/test/test_sqlite3/test_dump.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ def test_table_dump(self):
5656
[self.assertEqual(expected_sqls[i], actual_sqls[i])
5757
for i in range(len(expected_sqls))]
5858

59+
def test_dump_single_quote_in_identifier(self):
60+
# A single quote in a table or column name must not break the dump.
61+
self.cu.execute("""CREATE TABLE "a'b" ("c'd" text);""")
62+
self.cu.execute("""INSERT INTO "a'b" VALUES('x''y');""")
63+
expected = [
64+
"BEGIN TRANSACTION;",
65+
"""CREATE TABLE "a'b" ("c'd" text);""",
66+
"""INSERT INTO "a'b" VALUES('x''y');""",
67+
"COMMIT;",
68+
]
69+
actual = list(self.cx.iterdump())
70+
self.assertEqual(expected, actual)
71+
# The dump restores into a fresh database.
72+
with memory_database() as cx2:
73+
cx2.executescript("".join(actual))
74+
row = cx2.execute("""SELECT "c'd" FROM "a'b";""").fetchone()
75+
self.assertEqual(row[0], "x'y")
76+
5977
def test_table_dump_filter(self):
6078
all_table_sqls = [
6179
"""CREATE TABLE "some_table_2" ("id_1" INTEGER);""",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :meth:`sqlite3.Connection.iterdump` raising :exc:`sqlite3.OperationalError`
2+
when a table name contains a single quote. Patch by tonghuaroot.

0 commit comments

Comments
 (0)