Skip to content

jrwils/pysqlexec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

pysqlexec

PostreSQL at the Python prompt

pysqlexec is a functional way to communicate with a PostgreSQL database through Python. It uses the psycopg2 adapter.

Setup

Enter your database credentials in the dbs dictionary located in dbconfig.py. For example:

dbs = {'default':
   {'dbname': 'mydb',
    'dbuser': 'myuser',
    'dbpass': 'mypass',
    'dbhost': 'myhost',
    'dbport': 'myport'}}

More than one database can be added to the dbs dictionary. The default database will be used if database is not passed as a keyword argument in the function. For example, the following query would connect to the default database:

data = sqlexec("select * from mytable limit 1")

While this query would connect to the other database (provided it was defined in dbs):

data = sqlexec("select * from mytable limit 1", database="other")

Usage Examples

from pysqlexec.sqlexec import sqlexec

# Pass query parameters with string formatting

data = sqlexec("select * from people where firstname = %s", ("John",))

# Insertions

sqlexec("insert into states ('stateshort', 'statelong') values (%s, %s)", ('CA', 'California',))

For full documentation on passing parameters to queries, see the psycopg2 documentation

Written and tested with

Python 3.4

PostgreSQL 9.4

About

PostreSQL at the Python prompt

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages