7
7
# future enhancements, you should normally quote any identifier that
8
8
# is an English language word, even if you do not have to."
9
9
10
+ def _quote_name (name ):
11
+ return '"{0}"' .format (name .replace ('"' , '""' ))
12
+
13
+
14
+ def _quote_value (value ):
15
+ return "'{0}'" .format (value .replace ("'" , "''" ))
16
+
17
+
10
18
def _iterdump (connection ):
11
19
"""
12
20
Returns an iterator to the dump of the database in an SQL text format.
@@ -31,11 +39,11 @@ def _iterdump(connection):
31
39
sqlite_sequence = []
32
40
for table_name , type , sql in schema_res .fetchall ():
33
41
if table_name == 'sqlite_sequence' :
34
- rows = cu .execute ('SELECT * FROM "sqlite_sequence";' ). fetchall ()
42
+ rows = cu .execute ('SELECT * FROM "sqlite_sequence";' )
35
43
sqlite_sequence = ['DELETE FROM "sqlite_sequence"' ]
36
44
sqlite_sequence += [
37
- f'INSERT INTO "sqlite_sequence" VALUES(\' { row [ 0 ] } \' , { row [ 1 ] } )'
38
- for row in rows
45
+ f'INSERT INTO "sqlite_sequence" VALUES({ _quote_value ( table_name ) } , { seq_value } )'
46
+ for table_name , seq_value in rows . fetchall ()
39
47
]
40
48
continue
41
49
elif table_name == 'sqlite_stat1' :
@@ -53,12 +61,15 @@ def _iterdump(connection):
53
61
yield ('{0};' .format (sql ))
54
62
55
63
# Build the insert statement for each row of the current table
56
- table_name_ident = table_name . replace ( '"' , '""' )
57
- res = cu .execute ('PRAGMA table_info("{0}")' . format ( table_name_ident ) )
64
+ table_name_ident = _quote_name ( table_name )
65
+ res = cu .execute (f 'PRAGMA table_info({ table_name_ident } )' )
58
66
column_names = [str (table_info [1 ]) for table_info in res .fetchall ()]
59
- q = """ SELECT 'INSERT INTO " {0}" VALUES({1})' FROM " {0}";"" " .format (
67
+ q = "SELECT 'INSERT INTO {0} VALUES(' {1}' )' FROM {0}; " .format (
60
68
table_name_ident ,
61
- "," .join ("""'||quote("{0}")||'""" .format (col .replace ('"' , '""' )) for col in column_names ))
69
+ "','" .join (
70
+ "||quote({0})||" .format (_quote_name (col )) for col in column_names
71
+ )
72
+ )
62
73
query_res = cu .execute (q )
63
74
for row in query_res :
64
75
yield ("{0};" .format (row [0 ]))
0 commit comments