Back to index

python3.2  3.2.2
Functions
sqlite3.dump Namespace Reference

Functions

def _iterdump

Function Documentation

def sqlite3.dump._iterdump (   connection) [private]
Returns an iterator to the dump of the database in an SQL text format.

Used to produce an SQL dump of the database.  Useful to save an in-memory
database for later restoration.  This function should not be called
directly but instead called from the Connection method, iterdump().

Definition at line 4 of file dump.py.

00004 
00005 def _iterdump(connection):
00006     """
00007     Returns an iterator to the dump of the database in an SQL text format.
00008 
00009     Used to produce an SQL dump of the database.  Useful to save an in-memory
00010     database for later restoration.  This function should not be called
00011     directly but instead called from the Connection method, iterdump().
00012     """
00013 
00014     cu = connection.cursor()
00015     yield('BEGIN TRANSACTION;')
00016 
00017     # sqlite_master table contains the SQL CREATE statements for the database.
00018     q = """
00019         SELECT name, type, sql
00020         FROM sqlite_master
00021             WHERE sql NOT NULL AND
00022             type == 'table'
00023         """
00024     schema_res = cu.execute(q)
00025     for table_name, type, sql in schema_res.fetchall():
00026         if table_name == 'sqlite_sequence':
00027             yield('DELETE FROM sqlite_sequence;')
00028         elif table_name == 'sqlite_stat1':
00029             yield('ANALYZE sqlite_master;')
00030         elif table_name.startswith('sqlite_'):
00031             continue
00032         # NOTE: Virtual table support not implemented
00033         #elif sql.startswith('CREATE VIRTUAL TABLE'):
00034         #    qtable = table_name.replace("'", "''")
00035         #    yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
00036         #        "VALUES('table','%s','%s',0,'%s');" %
00037         #        qtable,
00038         #        qtable,
00039         #        sql.replace("''"))
00040         else:
00041             yield('%s;' % sql)
00042 
00043         # Build the insert statement for each row of the current table
00044         res = cu.execute("PRAGMA table_info('%s')" % table_name)
00045         column_names = [str(table_info[1]) for table_info in res.fetchall()]
00046         q = "SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("
00047         q += ",".join(["'||quote(" + col + ")||'" for col in column_names])
00048         q += ")' FROM '%(tbl_name)s'"
00049         query_res = cu.execute(q % {'tbl_name': table_name})
00050         for row in query_res:
00051             yield("%s;" % row[0])
00052 
00053     # Now when the type is 'index', 'trigger', or 'view'
00054     q = """
00055         SELECT name, type, sql
00056         FROM sqlite_master
00057             WHERE sql NOT NULL AND
00058             type IN ('index', 'trigger', 'view')
00059         """
00060     schema_res = cu.execute(q)
00061     for name, type, sql in schema_res.fetchall():
00062         yield('%s;' % sql)
00063 
00064     yield('COMMIT;')