How do cursors work in Python’s DB-API?

ya, i know it’s months old 😛 DB-API’s cursor appears to be closely modeled after SQL cursors. AFA resource(rows) management is concerned, DB-API does not specify whether the client must retrieve all the rows or DECLARE an actual SQL cursor. As long as the fetchXXX interfaces do what they’re supposed to, DB-API is happy. AFA … Read more

psycopg2 equivalent of mysqldb.escape_string?

Escaping is automatic, you just have to call: cursor.execute(“query with params %s %s”, (“param1”, “pa’ram2”)) (notice that the python % operator is not used) and the values will be correctly escaped. You can escape manually a variable using extensions.adapt(var), but this would be error prone and not keep into account the connection encoding: it is … Read more

psycopg2 leaking memory after large query

I ran into a similar problem and after a couple of hours of blood, sweat and tears, found the answer simply requires the addition of one parameter. Instead of cursor = conn.cursor() write cursor = conn.cursor(name=”my_cursor_name”) or simpler yet cursor = conn.cursor(“my_cursor_name”) The details are found at http://initd.org/psycopg/docs/usage.html#server-side-cursors I found the instructions a little confusing … Read more

python postgres can I fetchall() 1 million rows?

The solution Burhan pointed out reduces the memory usage for large datasets by only fetching single rows: row = cursor.fetchone() However, I noticed a significant slowdown in fetching rows one-by-one. I access an external database over an internet connection, that might be a reason for it. Having a server side cursor and fetching bunches of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)