Top-rated recipes tagged "sql"http://code.activestate.com/recipes/tags/sql/top/2016-07-23T22:17:29-07:00ActiveState Code RecipesPrinting 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> 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> Populate SQL tables from CSV data files (Python) 2011-02-02T21:21:49-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577559-populate-sql-tables-from-csv-data-files/ <p style="color: grey"> Python recipe 577559 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>Just a quick recipe I developed a few years ago that I thought might be useful to others. Basically it takes as input a data file with comma separated values (CSV) and translates this into a series of SQL "INSERT" statements allowing you to then feed this into MySQL, SQLite, or any other database.</p> <p>Example Usage:</p> <p>$ cat cars.csv Year,Make,Model,Length 1997,Ford,E350,2.34 2000,Mercury,Cougar,2.38</p> <p>$ sqlite3 cars.db "CREATE TABLE cars (Year, Make, Model, Length)"</p> <p>$ ./csv2sql.py cars.csv | sqlite3 cars.db </p> <p>$ sqlite3 cars.db "SELECT * FROM cars" 1997|Ford|E350|2.34 2000|Mercury|Cougar|2.38</p> <p>Enjoy! Feedback welcome!</p> <p>cheers James Mills / prologic</p> Minimalistic PostgreSQL console shell (Python) 2011-11-30T13:43:28-08:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577468-minimalistic-postgresql-console-shell/ <p style="color: grey"> Python recipe 577468 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/postgres/">postgres</a>, <a href="/recipes/tags/postgresql/">postgresql</a>, <a href="/recipes/tags/sql/">sql</a>). Revision 17. </p> <p>Primitive shell for running PostgreSQL quieries from console. Make sure you set the right connection settings in the script.</p> <p>All queries are executed in autocommit mode.</p> <p>Example command line usage:</p> <pre class="prettyprint"><code>pg.py select * from mytable where oidh=1 </code></pre> <p>The script recognizes a number of magic keywords:</p> <pre class="prettyprint"><code>pg.py show databases pg.py show schemas pg.py describe &lt;table_name&gt; </code></pre> 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 SQLite data to PDF using named tuples (Python) 2015-02-24T22:08:11-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579027-publish-sqlite-data-to-pdf-using-named-tuples/ <p style="color: grey"> Python recipe 579027 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/sqlite3/">sqlite3</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to publish SQLite data to PDF, using named tuples from the collections module of Python, the sqlite3 library, and the xtopdf library for PDF generation.</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> 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 (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> 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> Conjunction select using foreign keys (Text) 2011-06-11T02:43:22-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577747-conjunction-select-using-foreign-keys/ <p style="color: grey"> Text recipe 577747 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/conjunction/">conjunction</a>, <a href="/recipes/tags/foreign/">foreign</a>, <a href="/recipes/tags/key/">key</a>, <a href="/recipes/tags/select/">select</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>Say we have a table (notes) containing rows we want to select. Each note has one or more keywords (stored in a table of the same name). We want to select notes that have a conjunction of keywords (AND). notes and keywords are linked through a foreign key table notes_keywords. The following SQL statement allows us to do this</p> A MSSQL XML importer for MySQL (Python) 2011-03-09T03:46:49-08:00Albert Perrien IIhttp://code.activestate.com/recipes/users/4177266/http://code.activestate.com/recipes/577601-a-mssql-xml-importer-for-mysql/ <p style="color: grey"> Python recipe 577601 by <a href="/recipes/users/4177266/">Albert Perrien II</a> (<a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/server/">server</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>This is a simple function that takes records exported to XML from SQL Server or Access and imports them into MySQL. </p> ExecSql.cgi returning JSON for SQL (Python) 2009-11-28T02:00:30-08:00Martchenkohttp://code.activestate.com/recipes/users/4172446/http://code.activestate.com/recipes/576971-execsqlcgi-returning-json-for-sql/ <p style="color: grey"> Python recipe 576971 by <a href="/recipes/users/4172446/">Martchenko</a> (<a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/sql/">sql</a>). </p> <p>CGI script getting JSON for SQL request</p> Transparently execute SQL queries as prepared statements with Postgresql (Python) 2009-03-23T07:30:07-07:00Michael Palmerhttp://code.activestate.com/recipes/users/1827292/http://code.activestate.com/recipes/576698-transparently-execute-sql-queries-as-prepared-stat/ <p style="color: grey"> Python recipe 576698 by <a href="/recipes/users/1827292/">Michael Palmer</a> (<a href="/recipes/tags/postgresql/">postgresql</a>, <a href="/recipes/tags/prepared_statements/">prepared_statements</a>, <a href="/recipes/tags/sql/">sql</a>). Revision 4. </p> <p>This recipe defines a mixin class for DBAPI cursors that gives them an 'executeps' method. This method transparently converts any SQL query into a prepared statement, which gets cached and executed instead of the original query.</p>