Popular recipes tagged "database"http://code.activestate.com/recipes/tags/database/2016-09-29T18:04:57-07:00ActiveState Code RecipesPut Peewee ORM data to PDF with xtopdf (Python) 2016-09-29T18:04:57-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580704-put-peewee-orm-data-to-pdf-with-xtopdf/ <p style="color: grey"> Python recipe 580704 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/orm/">orm</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/peewee/">peewee</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how some basics of how to fetch data from database tables managed by the Peewee ORM (a lightweight expressive ORM for Python) and write that data, formatted, to a PDF file. The recipe uses Python, the Peewee ORM and the xtopdf toolkit for PDF creation.</p> DBF reader and writer -- selective fields and nullreplace (Python) 2016-09-18T20:39:20-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/580696-dbf-reader-and-writer-selective-fields-and-nullrep/ <p style="color: grey"> Python recipe 580696 by <a href="/recipes/users/4189558/">Tomas Nordin</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>This fork assumes a desire for limited selection of field names. With huge files this might be necessary on some machines.</p> <p>Also, assuming that the meaning of null in a dbf file means zero might be a mistake, so the fork adds an argument nullreplace as way to choose what to replace null with. Null is sometimes used to mean missing value. This change is decoupled from the selective names feature.</p> Create PDF control break reports with itertools.groupby and xtopdf (Python) 2016-07-23T22:17:29-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580692-create-pdf-control-break-reports-with-itertoolsgro/ <p style="color: grey"> Python recipe 580692 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/control/">control</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportgeneration/">reportgeneration</a>, <a href="/recipes/tags/reporting/">reporting</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to create the classic control break style of report (a staple of data processing) using Python along with the groupby function from the itertools module, and xtopdf, a Python toolkit for PDF creation.</p> Publish Firebird SQL data to PDF with xtopdf (Python) 2015-12-01T19:45:18-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579131-publish-firebird-sql-data-to-pdf-with-xtopdf/ <p style="color: grey"> Python recipe 579131 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/firebird/">firebird</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to publish data from the Firebird RDBMS to PDF, using the xtopdf toolkit and the fbd Python driver for Firebird. Firebird is a cross-platform, open source RDBMS based on the former Interbase RDBMS from Borland, which they used to bundle with some of their developmemt tools, such as Borland C++ and Borland Delphi.</p> <p>The recipe reads data from a Firebird table, using the fbd Python driver for Firebird, and writes it to PDF, using the xtopdf toolkit. See:</p> <p><a href="http://jugad2.blogspot.in/p/xtopdf-pdf-creation-library.html" rel="nofollow">http://jugad2.blogspot.in/p/xtopdf-pdf-creation-library.html</a></p> <p>for information on xtopdf.</p> <p>It assumes that a Firebird database called test.fdb exists under /temp/firebird (C:\temp\firebird, really - the test was done on Windows), and that it has a contacts table with the structure shown in the code of the recipe.</p> <p>More details and sample output are here:</p> <p><a href="http://jugad2.blogspot.in/2014/01/by-vasudev-ram-pdf-firebird-is-cross.html" rel="nofollow">http://jugad2.blogspot.in/2014/01/by-vasudev-ram-pdf-firebird-is-cross.html</a></p> Publish databases to PDF with PyDAL (a Python DB library) and xtopdf (Python) 2015-01-15T16:09:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579004-publish-databases-to-pdf-with-pydal-a-python-db-li/ <p style="color: grey"> Python recipe 579004 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pydal/">pydal</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_developer_tools/">python_developer_tools</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/web2py/">web2py</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to use the PyDAL database library for Python, together with xtopdf, a PDF creation library for Python, to publish database data to PDF. PyDAL was earlier a part of the web2py Python web framework, and then was split out into a separate library that does not need to be used with web2py. It supports access to many popular relational databases, both open source and proprietary.</p> Python SQLSMO - Threading Example (Python) 2015-02-10T02:01:42-08:00Jorge Besadahttp://code.activestate.com/recipes/users/100038/http://code.activestate.com/recipes/579023-python-sqlsmo-threading-example/ <p style="color: grey"> Python recipe 579023 by <a href="/recipes/users/100038/">Jorge Besada</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/smo/">smo</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/sqlsmo/">sqlsmo</a>, <a href="/recipes/tags/threading/">threading</a>). </p> <p>This is an example of the use of the SQLSMO module. Using a csv file DBLIST_ACTIONS.csv with list of databases where you can launch multiple different database operations in parallel</p> <p>Some lines of the configuration file DBLIST_ACTIONS.csv used shown below: SERVERNAME,DBNAME1,SOURCESERVER,DATAFOLDER,LOGFOLDER,DBNAME2,ACTIONS,ENABLED (local)\sql2014,AdventureWorks2012,C:\SQL2014\BACKUPS,C:\SQL2014\DATA,C:\SQL2014\LOG,AdventureWorks_COPY1,RESTOREDBS1.CFG,Y (local)\sql2014,AdventureWorks2012,C:\SQL2014\BACKUPS,C:\SQL2014\DATA,C:\SQL2014\LOG,AdventureWorks_COPY2,RESTOREDBS1.CFG,Y (local)\sql2014,AdventureWorks2012,C:\SQL2014\BACKUPS,C:\SQL2014\DATA,C:\SQL2014\LOG,AdventureWorks_COPY3,RESTOREDBS1.CFG,Y (local)\sql2014,AdventureWorks2012,C:\SQL2014\BACKUPS,C:\SQL2014\DATA,C:\SQL2014\LOG,AdventureWorks_COPY4,RESTOREDBS1.CFG,</p> <p>Where: SERVERNAME: server where the database to act upon resides</p> <p>DBNAME1: source database</p> <p>DBNAME2: destination database (may be different from source when we restore a copy with a different name)</p> <p>SOURCESERVER: this is the network (or local) folder where backups are placed</p> <p>DATAFOLDER: folder for data files</p> <p>LOGFOLDER: folder for log files</p> <p>ACTIONS: this is the name of the configuration file (.CFG) with the list of actions</p> <p>ENABLED: a Y value here will mean we want to process the line</p> <p>For each line (database) you specify a configuration file (in this case RESTOREDBS1.CFG), see sample below: (one line for each, no blank lines)</p> <p>RESTORE DATABASE</p> <p>SET DBOWNER: sa</p> <p>SYNC LOGINS</p> <p>SET SIMPLE MODE</p> <p>SHRINK LOG</p> <p>The program will process each line in the source CSV file and for each one it will perform the set of operations described in the configuration file. This system is being used in my workplace with different configuration files for different databases (there are configuration files for restores, specific restores with more actions, backups, etc, not included here for brevity).</p> <p>Every time you do a database task you just add a line in the DBLIST_ACTIONS.CSV and create (if needed) a configuration file). If you are going to actually use it include a "Y" in the ENABLED column Note: every line action in the configuration file must have been implemented in the function ActionParsing as one the entries in the big if statement.</p> <p>Special features: You can specify at the start of the program if you want to really execute or not. You may want to do first a trial run setting NOEXECUTE_OPTION = 1 (instead of the default of 0). In this case the program will run and create the SQL script of the operations, not executing them. Note: it has been implemented in the restores so far, will add it to the other options later.</p> <p>Threading: by default it will run as many threads as lines in the DBLIST_ACTIONS.CSV file. But you can change this option by setting a value to THREAD_POOL different than 0. </p> Python ADO Database Interface for MS SQL Server - Python 3 version 1.0 (Python) 2014-08-27T16:04:04-07:00Jorge Besadahttp://code.activestate.com/recipes/users/100038/http://code.activestate.com/recipes/578913-python-ado-database-interface-for-ms-sql-server-py/ <p style="color: grey"> Python recipe 578913 by <a href="/recipes/users/100038/">Jorge Besada</a> (<a href="/recipes/tags/ado/">ado</a>, <a href="/recipes/tags/database/">database</a>). Revision 3. </p> <p>This DBI implements the Cursor and Connection objects. You can create connections, cursors, do fetchone, fetchall. It uses ADO. Will add more features later </p> Python SQLSMO (Python) 2015-03-28T16:58:11-07:00Jorge Besadahttp://code.activestate.com/recipes/users/100038/http://code.activestate.com/recipes/578977-python-sqlsmo/ <p style="color: grey"> Python recipe 578977 by <a href="/recipes/users/100038/">Jorge Besada</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/smo/">smo</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/sqlsmo/">sqlsmo</a>). Revision 15. </p> <p>I needed a Python library equivalent to the SQL Server Management Objects (SMO) and did not find it, so I created my own version. It does not follow the standard SMO objects names. So far it has a set of basic functionality: to make backups, restores with move, sync logins for restored databases, check disk space. I included a good sized testing harness to get you going. This version uses sqlcmd for connectivity. I use this SQLSMO library as an imported module in several of my Python applications. It has been tested with SQL 2012 and SQL 2014, it should function with versions down to 2005.</p> Printing list of ODBC data sources with pyodbc module (Python) 2014-01-28T10:40:56-08:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/578815-printing-list-of-odbc-data-sources-with-pyodbc-mod/ <p style="color: grey"> Python recipe 578815 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/odbc/">odbc</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>I have similar recipe but with <code>odbc</code> module: <a href="http://code.activestate.com/recipes/578782-printing-list-of-odbc-data-sources/" rel="nofollow">http://code.activestate.com/recipes/578782-printing-list-of-odbc-data-sources/</a></p> Python Database Interface for MS SQL Server - Python 3 version (Python) 2014-07-11T19:25:48-07:00Jorge Besadahttp://code.activestate.com/recipes/users/100038/http://code.activestate.com/recipes/578906-python-database-interface-for-ms-sql-server-python/ <p style="color: grey"> Python recipe 578906 by <a href="/recipes/users/100038/">Jorge Besada</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>This DBI implements the Cursor and Connection objects. It is functional: you can create connections, cursors, do fetchone, fetchall, get rowcount, etc. It uses osql or sqlcmd instead of ODBC or ADO. There is a good sized section with examples to get you started. </p> Publish Berkeley DB data to PDF with xtopdf (Python) 2014-01-24T17:57:21-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578814-publish-berkeley-db-data-to-pdf-with-xtopdf/ <p style="color: grey"> Python recipe 578814 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/bsddb/">bsddb</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to convert the data stored in a Berkeley DB database to PDF, using the xtopdf toolkit for PDF creation. It should work on either Linux/Unix or Windows.</p> Printing list of ODBC data sources (Python) 2013-12-10T11:07:37-08:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/578782-printing-list-of-odbc-data-sources/ <p style="color: grey"> Python recipe 578782 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/odbc/">odbc</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>This simple code shows ODBC data sources. It uses <code>odbc</code> module.</p> A Phone Book GUI Built in wxPython Connected To Database Using Data Grid View (Python) 2013-09-29T19:25:23-07:00toufic zaarourhttp://code.activestate.com/recipes/users/4187866/http://code.activestate.com/recipes/578676-a-phone-book-gui-built-in-wxpython-connected-to-da/ <p style="color: grey"> Python recipe 578676 by <a href="/recipes/users/4187866/">toufic zaarour</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/datagridview/">datagridview</a>, <a href="/recipes/tags/graphical/">graphical</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/interface/">interface</a>). </p> <p>this GUI as simple as it is explains some basic but important graphical database interfacing; "Add", "Edit","Delete","Search" and few others along with a data grid view. in order to work create an sqlite3 database as follows:</p> <p>data table : Phone, column 1 : ID, column 2 : name, column 3 : surname, column 4 : telephone.</p> <p>save the sqlite3 file as file.db in a folder called Data and place it in the same directory as your python script.</p> <p>if you want to create the sqlite3 database graphically use my previous post : <a href="http://code.activestate.com/recipes/578665-a-wxpython-gui-to-create-sqlite3-databases/" rel="nofollow">http://code.activestate.com/recipes/578665-a-wxpython-gui-to-create-sqlite3-databases/</a></p> <p>Also there is more: I did not use auto-number for 'id' because I also wanted to include in the code a renumbering script.</p> <p>I am pleased to receive all the suggestions and improvements on this site or to my e-mail directly if this is convenient to you.</p> <p>note: if you don't like the database table name, and columns name create your own but make sure to change them in the code as well! in the end life is great! remember that!</p> List MySql databases in a Gtk.TreeView (Python) 2013-11-27T14:03:21-08:00Anonimistahttp://code.activestate.com/recipes/users/4188571/http://code.activestate.com/recipes/578774-list-mysql-databases-in-a-gtktreeview/ <p style="color: grey"> Python recipe 578774 by <a href="/recipes/users/4188571/">Anonimista</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/mysqldb/">mysqldb</a>, <a href="/recipes/tags/user_interface/">user_interface</a>). </p> <p>List MySql databases in a Gtk.TreeView</p> Save and restore SHA-512 internal state (Python) 2013-03-03T18:05:42-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578479-save-and-restore-sha-512-internal-state/ <p style="color: grey"> Python recipe 578479 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/inner/">inner</a>, <a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/restore/">restore</a>, <a href="/recipes/tags/sha512/">sha512</a>, <a href="/recipes/tags/state/">state</a>). </p> <p>If you have a very long input to hash, you may want to save your progress.</p> <p>CPython doesn't normally let you, but it's easy to hack around via ctypes</p> sql+ the SQL*Plus killer (Python) 2013-03-14T09:13:01-07:00johttp://code.activestate.com/recipes/users/4185610/http://code.activestate.com/recipes/578490-sql-the-sqlplus-killer/ <p style="color: grey"> Python recipe 578490 by <a href="/recipes/users/4185610/">jo</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/db/">db</a>, <a href="/recipes/tags/db_client/">db_client</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/sql_client/">sql_client</a>). Revision 3. </p> <p>This recipe is an emulator of the Oracle SQL*Plus, but it does things in a more friendly way ;).</p> <p>If you need a client to access your Oracle but you don't like SQL*Plus, try this one.</p> <p>This recipe was inspired by James Thiele's Console built with Cmd object recipe.</p> <p>It provides a 'help' facility and supplies command completion when you hit the 'tab' key.</p> <p>In addition you can use command line editing and history keys.</p> <p>Here are the commands that you can use:</p> <pre class="prettyprint"><code>======================================== Documented commands (type help &lt;topic&gt;): ======================================== constraints edit help input sequences shell db exit history output set tables desc foreigns index quit settings triggers </code></pre> <p>You can edit the queries using 'vim' or any other editor.</p> <p>The command 'output' can redirect output to a file and command 'input' can input commands from a file.</p> <p>There's paginated output.</p> <p>The command 'shell' or '!' allow you to perform operating system commands.</p> <p>You can add commands by defining methods with names of the form 'do_xxx()' where 'xxx' is the name of the command you wish to add.</p> <p>There is a configuration file (.sql+) where you need to enter the dburi, editor name and other settings.</p> Profile Manager (Cave Story) (Python) 2012-12-07T01:21:04-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578368-profile-manager-cave-story/ <p style="color: grey"> Python recipe 578368 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/profile/">profile</a>). </p> <p>If you have ever played a game that only had one save slot and wanted to be able to manage profiles, the following code written for Cave Story may be of use to you. The recipe provides a starting point for how one might go about writing a profile manager for such a program that runs through a command interface.</p> Saving snippets to SQLite3 database (Python) 2014-07-30T07:23:36-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578285-saving-snippets-to-sqlite3-database/ <p style="color: grey"> Python recipe 578285 by <a href="/recipes/users/4183895/">p@ntut$</a> (<a href="/recipes/tags/database/">database</a>). Revision 5. </p> <p>Save your snippets/codes to sqlite3 database, search, edit, and delete.</p> Read tabular data from Excel spreadsheets the fast and easy way (Python) 2012-10-09T07:00:55-07:00wei.Liuhttp://code.activestate.com/recipes/users/4183853/http://code.activestate.com/recipes/578283-read-tabular-data-from-excel-spreadsheets-the-fast/ <p style="color: grey"> Python recipe 578283 by <a href="/recipes/users/4183853/">wei.Liu</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>Sometimes you get an Excel spreadsheet (say, from the marketing departement) and you want to read tabular data from it (i.e. a line with column headers and lines of data). There are many ways to do this (including ODBC + mxODBC), but the easiest way I've found is this one : provide a file name and a sheet name, and read the data !</p> Inner Join (Python) 2011-11-01T20:10:34-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577937-inner-join/ <p style="color: grey"> Python recipe 577937 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/sql/">sql</a>). Revision 2. </p> <p>Implemented an SQL style INNER JOIN for two lists of tuples to be joined on a common field.</p>