Questions tagged [python-db-api]

Questions about how to use the Python Database API Specification 2.0 -- PEP 249. Do include details about your database library

This API has been defined to encourage similarity between the Python modules that are used to access databases. By doing this, we hope to achieve a consistency leading to more easily understood modules, code that is generally more portable across databases, and a broader reach of database connectivity from Python.

See: https://www.python.org/dev/peps/pep-0249/

134 questions
9
votes
2 answers

Identifying sqlalchemy.exc.OperationalError

I'm trying to catch mysql/sqlalchemy OperationalErrors and replace handle access denied (1045) differently from connection refused (2003) sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user … (Background on…
Gamification
  • 787
  • 5
  • 20
9
votes
2 answers

How to specify psycopg2 parameter for an array for timestamps (datetimes)

I'd like to run a PostgreSQL query in Python using psycopg2, which filters by a column of type timestamp without timezone. I have a long list of allowed values for the timestamp (rather than a range) and psycopg2 conveniently handles arrays, so I…
EMP
  • 59,148
  • 53
  • 164
  • 220
8
votes
2 answers

Why is Twisted's adbapi failing to recover data from within unittests?

Overview Context I am writing unit tests for some higher-order logic that depends on writing to an SQLite3 database. For this I am using twisted.trial.unittest and twisted.enterprise.adbapi.ConnectionPool. Problem statement I am able to create a…
Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
8
votes
2 answers

Why connection in Python's DB-API does not have "begin" operation?

Working with cursors in mysql-python I used to call "BEGIN;", "COMMIT;", and "ROLLBACK;" explicitly as follows: try: cursor.execute("BEGIN;") # some statements cursor.execute("COMMIT;") except: cursor.execute("ROLLBACK;") then, I…
newtover
  • 31,286
  • 11
  • 84
  • 89
8
votes
1 answer

Python MySQL Connector database query with %s fails

I have a basic program that is supposed to query a database that contains user information. I am trying to select the information for a specific user and print it out to the console. Here is my code: import mysql.connector funcon =…
8
votes
2 answers

Do cursors in Django run inside the open transaction?

My Django application is using some custom SQL which I am executing inside a view like this: db = router.db_for_write(model) cursor = connections[db].cursor() cursor.execute("INSERT INTO ....") Since I am using the TransactionMiddleware, my view is…
gerdemb
  • 11,275
  • 17
  • 65
  • 73
7
votes
2 answers

sql print statements from pyodbc

How do I get the output from the sql_query? import pyodbc sql_query = "print 'Hello World'" conn = pyodbc.connect("DRIVER={SQL Server}; SERVER=myserver; DATABASE=mydatabase; UID=myusername; PWD=mypassword") cur =…
ijk
  • 175
  • 1
  • 7
6
votes
0 answers

Difference between JDBC driver vs Python Adapters for Amazon Redshift

To connect Amazon Redshift, I used the python psycopg2 module to inject the dataset on Amazon Redshift and it is working fine. Just to mention that I'm using the Redshift's endpoint URL to connect via the psycopg2 which underneath uses python DB API…
6
votes
1 answer

How do I check for open transactions on a psycopg2 connection?

How can I check for open transactions on a psycopg2 connection? I intend to add it to my unit/functional tests since Python's DB API uses implicit transactions.
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
6
votes
3 answers

Cancel query execution in pyscopg2

How would one go about cancelling execution of a query statement using pyscopg2 (the python Postgres driver)? As an example, let's say I have the following code: import psycopg2 cnx_string = "something_appropriate" conn =…
jwoolard
  • 6,024
  • 9
  • 37
  • 37
6
votes
1 answer

How to get prepared query which is sent to db

When using database libraries like pyodbc that implement the Python Database API Specification how can you get the fully prepared query after parameter substitution has been applied. I'm making a call to a Sybase stored procedure that will receive…
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65
6
votes
2 answers

sqlite3 and cursor.description

When using the sqlite3 module in python, all elements of cursor.description except the column names are set to None, so this tuple cannot be used to find the column types for a query result (unlike other DB-API compliant modules). Is the only way to…
astrofrog
  • 32,883
  • 32
  • 90
  • 131
6
votes
1 answer

Python DB API list tables

How do I list a DB's tables with Python's DB API? Failing that, is there another way to do it? Thanks
user1458476
  • 143
  • 2
  • 8
6
votes
1 answer

mysql-python: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist

I installed mysql using homebrew. I'm now trying to install mysql-python and I keep getting the error below when I run mysql. I'm new to programming and don't know enough to identify the problem. Any help will be appreciated. Thanks. …
TDNS
  • 4,125
  • 2
  • 17
  • 17
5
votes
1 answer

How to fetch data from Clickhouse in dicitionary/name-tuple using clickhouse-driver (python)?

When we fetch data using the DB API 2.0 cur.execute("select * from db.table") we get a cursor which seems like a generator object of list of tuples. Whereas in pymongo, when we fetch we get it as list of dictionaries. I wanted to achieve something…
1
2
3
8 9