Changeset 391 for python/trunk/Lib/sqlite3/dump.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/sqlite3/dump.py
r2 r391 1 1 # Mimic the sqlite3 console shell's .dump command 2 2 # Author: Paul Kippes <kippesp@gmail.com> 3 4 # Every identifier in sql is quoted based on a comment in sqlite 5 # documentation "SQLite adds new keywords from time to time when it 6 # takes on new features. So to prevent your code from being broken by 7 # future enhancements, you should normally quote any identifier that 8 # is an English language word, even if you do not have to." 3 9 4 10 def _iterdump(connection): … … 16 22 # sqlite_master table contains the SQL CREATE statements for the database. 17 23 q = """ 18 SELECT name, type, sql 19 FROM sqlite_master 20 WHERE sql NOT NULL AND 21 type == 'table' 24 SELECT "name", "type", "sql" 25 FROM "sqlite_master" 26 WHERE "sql" NOT NULL AND 27 "type" == 'table' 28 ORDER BY "name" 22 29 """ 23 30 schema_res = cu.execute(q) 24 31 for table_name, type, sql in schema_res.fetchall(): 25 32 if table_name == 'sqlite_sequence': 26 yield('DELETE FROM sqlite_sequence;')33 yield('DELETE FROM "sqlite_sequence";') 27 34 elif table_name == 'sqlite_stat1': 28 yield('ANALYZE sqlite_master;')35 yield('ANALYZE "sqlite_master";') 29 36 elif table_name.startswith('sqlite_'): 30 37 continue … … 33 40 # qtable = table_name.replace("'", "''") 34 41 # yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\ 35 # "VALUES('table',' %s','%s',0,'%s');" %42 # "VALUES('table','{0}','{0}',0,'{1}');".format( 36 43 # qtable, 37 # qtable, 38 # sql.replace("''")) 44 # sql.replace("''"))) 39 45 else: 40 46 yield('%s;' % sql) 41 47 42 48 # Build the insert statement for each row of the current table 43 res = cu.execute("PRAGMA table_info('%s')" % table_name) 49 table_name_ident = table_name.replace('"', '""') 50 res = cu.execute('PRAGMA table_info("{0}")'.format(table_name_ident)) 44 51 column_names = [str(table_info[1]) for table_info in res.fetchall()] 45 q = " SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("46 q += ",".join(["'||quote(" + col + ")||'" for col in column_names])47 q += ")' FROM '%(tbl_name)s'"48 query_res = cu.execute(q % {'tbl_name': table_name})52 q = """SELECT 'INSERT INTO "{0}" VALUES({1})' FROM "{0}";""".format( 53 table_name_ident, 54 ",".join("""'||quote("{0}")||'""".format(col.replace('"', '""')) for col in column_names)) 55 query_res = cu.execute(q) 49 56 for row in query_res: 50 57 yield("%s;" % row[0]) … … 52 59 # Now when the type is 'index', 'trigger', or 'view' 53 60 q = """ 54 SELECT name, type, sql55 FROM sqlite_master56 WHERE sqlNOT NULL AND57 typeIN ('index', 'trigger', 'view')61 SELECT "name", "type", "sql" 62 FROM "sqlite_master" 63 WHERE "sql" NOT NULL AND 64 "type" IN ('index', 'trigger', 'view') 58 65 """ 59 66 schema_res = cu.execute(q)
Note:
See TracChangeset
for help on using the changeset viewer.