This article example describes the Python implementation of the sqlite database export to Excel (xls) table method. Shared for your reference, as follows:
1. It is assumed that the Python environment with the sliqte library is already installed.
Mine is Python 2.5.
2. Download the python xls write package (xlwt) and install it.
Download at./pypi/xlwt
3. Here is the code ():
import sqlite3 as sqlite from xlwt import * #MASTER_COLS = ['rowid', 'type','name','tbl_name', 'rootpage','sql'] def sqlite_get_col_names(cur, table): query = 'select * from %s' % table (query) return [tuple[0] for tuple in ] def sqlite_query(cur, table, col = '*', where = ''): if where != '': query = 'select %s from %s where %s' % (col, table, where) else: query = 'select %s from %s ' % (col, table) (query) return () def sqlite_to_workbook(cur, table, workbook): ws = workbook.add_sheet(table) print 'create table %s.' % table for colx, heading in enumerate(sqlite_get_col_names(cur, table)): (0,colx, heading) for rowy,row in enumerate(sqlite_query(cur, table)): for colx, text in enumerate(row): (rowy+ 1, colx, text) def main(dbpath): xlspath = dbpath[0:('.')] + '.xls' print "<%s> --> <%s>"% (dbpath, xlspath) db = (dbpath) cur = () w = Workbook() for tbl_name in [row[0] for row in sqlite_query(cur, 'sqlite_master', 'tbl_name', 'type = \'table\'')]: sqlite_to_workbook(cur,tbl_name, w) () () if tbl_name !=[]: (xlspath) if __name__ == "__main__": # arg == database path main([1])
4. Usage.
> python <path>/ dbpath
If that's correct, an xls file with the same name will be generated in the database's directory
Readers interested in more Python related content can check out this site's topic: theSummary of common database manipulation techniques in Python》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.