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
1
vote
1 answer

parameterized query Postgres for sql injection mitigation

stmt = "SELECT filecontent FROM filecontent WHERE filecontent_id = %d AND filecontent LIKE '%% %s %%'"%(int(result_file_pri[0]),str(myjson['recordType'])) curs.execute(stmt) Trying to conevert above postgres query to parameterized query to itigate…
Pravin
  • 677
  • 2
  • 7
  • 22
1
vote
2 answers

What's the preferred way to handle transactions in pyscopg2?

According to psycopg2 documentation we should set autocommit to get the default PostgreSQL behaviour. This even seems to be the preferred approach according to some people. My question is, if this is the best approach in these circumstances, how do…
dvieira
  • 683
  • 1
  • 7
  • 19
1
vote
1 answer

Using parameters in a CREATE TABLE ... (column DEFAULT ?) clause, in Python sqlite3 db-api

I want to use python to execute a CREATE STATEMENT clause, specifying default values for certain columns using parameter substitution with ? (so that I can safely specify defaults from python types). It doesn't appear to work, although it works fine…
bryhoyt
  • 273
  • 3
  • 10
1
vote
1 answer

Postgresql: How to delete rows from table constrained by date?

I am using psycopg2 and how can I delete rows that are older than a certain date? For example: cursor.execute('DELETE FROM datatable WHERE date < %s', datetime.date(2012, 1, 1)) If I write like this, there will be TypeError: 'datetime.date' object…
Robert
  • 2,189
  • 6
  • 31
  • 38
1
vote
1 answer

Psycopg2 production appropriate mogrify?

I want to do exactly what cursor.mogrify does, but in a production appropriate way. I'm updating some legacy Python code that builds queries by concatenating strings. I need to change this to escape safely. The queries are long and built on a…
Tim Wilder
  • 1,607
  • 1
  • 18
  • 26
1
vote
0 answers

How do I lazily pass csv rows to executemany()?

I'm using MySQL Connector/Python 1.0.11 with Python 3.3 and MySQL 5.6.12 to load a csv file into a table via cursor.executemany('INSERT INTO ... VALUES'). With sqlite3 or psycopg2 I can pass a _csv.reader object for seq_of_parameters, but MySQL…
Matthew Cornell
  • 4,114
  • 3
  • 27
  • 40
1
vote
1 answer

Django/db-api broken for create database?

Was hoping to use Django / db-api's built in string excaping, but it looks like it doesn't work for create database commands? from django.db import connections cursor = connections['dbadmin'].cursor() cursor.execute('create database %s', ['foo'])…
hwjp
  • 15,359
  • 7
  • 71
  • 70
1
vote
1 answer

PostgreSQL/performance one general cursor or create for every query

I am building a script to store some data in a database. First time I'm using PostgeSQL and everything goes well and as planned. I was thinking about the usage of the Cursor in PostgreSQl and what if I am making a lot of them while one is enough.…
user1419979
1
vote
1 answer

Python libmysqlclient segfault (MySQLdb)

I am writing an SQL wrapper over MySQLdb. Sometime during execution I am getting the following error in DMSG and the process crushes: python[xxx]: segfault at c8 ip 00007fa1add6b882 sp 00007fa1abb77c60 error 4 in…
user1144031
  • 627
  • 2
  • 6
  • 16
0
votes
1 answer

Python MySQL data import

I am using the following script to pull data from a third party tool, create a table in a MySQL database and populate it with the resulting data. The script runs through and I can see the print out of all of the requested data in the Python Shell…
Ben
  • 153
  • 2
  • 6
  • 18
0
votes
0 answers

Is there an actual base class for a DBAPI Cursor?

Trying to write a method that takes a cursor with type hints. Something like this: def parse_rows(cursor: DBAPICursor): columns = cursor.description rows = [{columns[index][0]:column for index, column in enumerate(value)} for…
jhnclvr
  • 9,137
  • 5
  • 50
  • 55
0
votes
0 answers

List of required parameters for a query

How can I obtain a list of required DBAPI2 named parameters (sqlite3, psycopg) from a parametrized query (without executing it and seeing error message)? Something like that: query1 = "select * from mytable where id = ':id' and name =…
pieca
  • 2,463
  • 1
  • 16
  • 34
0
votes
1 answer

How to catch exception clickhouse_driver dbapi?

I want to catch exception while executing scipts/connecting to base using clickhouse_driver-drive dbapi. Can I catch errors codes and errors message like errorcodes.lookup(e.pgcode) and e.diag.message_primary from psycopg2.import errorcodes?
FeoJun
  • 103
  • 1
  • 14
0
votes
1 answer

Pandas dataframe to SQL table using presto-python-client syntax error: mismatched input ';'

I am connecting to a presto db, trying to write a dataframe into a sql table. I can "CREATE TABLE" but df.to_sql throws a syntax error: PrestoUserError: PrestoUserError(type=USER_ERROR, name=SYNTAX_ERROR, message="line 1:61: mismatched input ';'.…
noiivice
  • 400
  • 2
  • 15
0
votes
1 answer

How to preform insert into BigQuery table of list of values using dbapi

I have a question regarding performing insert into a table in BigQuery using the DBAPI . The simple insert of 1 value I was able to preform , my question is how to preform insert of list of values. I couldn't find an example on how to do it, I want…
Nir Elbaz
  • 556
  • 4
  • 19
1 2 3
8 9