psycopg2

SqlAlchemy: getting the id of the last record inserted

天涯浪子 提交于 2020-04-07 17:35:31
问题 I am using SQLAlchemy without the ORM, i.e. using hand crafted SQL statememts to directly interact with the backend db. I am using PG as my backend db (psycopg2 as DB driver) in this instance - I don't know if that affects the answer. I have statements like this (for brevity, assume that conn is a valid connection to the db): conn.execute("INSERT INTO user (name, country_id) VALUES ('Homer', 123)") Assume also that the user table consists of the columns (id [SERIAL PRIMARY KEY], name, country

What happened to psycopg2.sql?

老子叫甜甜 提交于 2020-03-24 00:43:59
问题 In the psycopg2 docs it references an sql property full of fun goodness for safely constructing SQL strings. Yet my install on python 3, which should be latest from PyPI, contains no such attribute: from psycopg2 import sql # ImportError: cannot import name 'sql' import psycopg2 print(dir(psycopg2)) # no mention of sql, although dir is not complete # in the REPL help(psycopg2) # no mention of the sql submodule that I saw So how can I safely construct SQL strings without it (I have zero

What happened to psycopg2.sql?

拜拜、爱过 提交于 2020-03-24 00:41:51
问题 In the psycopg2 docs it references an sql property full of fun goodness for safely constructing SQL strings. Yet my install on python 3, which should be latest from PyPI, contains no such attribute: from psycopg2 import sql # ImportError: cannot import name 'sql' import psycopg2 print(dir(psycopg2)) # no mention of sql, although dir is not complete # in the REPL help(psycopg2) # no mention of the sql submodule that I saw So how can I safely construct SQL strings without it (I have zero

Is it possible to catch a foreign key violation in postgres

二次信任 提交于 2020-03-23 02:05:11
问题 I'm trying to insert data into a table which has a foreign key constraint. If there is a constraint violation in a row that I'm inserting, I want to chuck that data away. The issue is that postgres returns an error every time I violate the constraint. Is it possible for me to have some statement in my insert statement like 'ON FOREIGN KEY CONSTRAINT DO NOTHING'? EDIT: This is the query that I'm trying to do, where info is a dict: cursor.execute("INSERT INTO event (case_number_id, date, \

Passing a datetime into psycopg2

*爱你&永不变心* 提交于 2020-03-22 09:22:31
问题 I am trying to insert a datetime value into my postgres database using Psycopg2. My code was working before, but I switched from %s notation to {} notation, and my code broke. here is my code for j in eveLists.itemList: tempPrice = fetchSellPrice(i, j) database_name = eveLists.DatabaseDict[i] now = datetime.datetime.utcnow() cur.execute("INSERT INTO {0} VALUES ({1}, {2}, {3}, NULL, {4}, {5}, NULL);".format( database_name, str(i), str(j), float(tempPrice), datetime.date.today(), now)) I get

Passing a datetime into psycopg2

牧云@^-^@ 提交于 2020-03-22 09:22:08
问题 I am trying to insert a datetime value into my postgres database using Psycopg2. My code was working before, but I switched from %s notation to {} notation, and my code broke. here is my code for j in eveLists.itemList: tempPrice = fetchSellPrice(i, j) database_name = eveLists.DatabaseDict[i] now = datetime.datetime.utcnow() cur.execute("INSERT INTO {0} VALUES ({1}, {2}, {3}, NULL, {4}, {5}, NULL);".format( database_name, str(i), str(j), float(tempPrice), datetime.date.today(), now)) I get

psycopg2: DictCursor vs RealDictCursor

时间秒杀一切 提交于 2020-03-21 11:55:07
问题 AFAIU and from docs, RealDictCursor is a specialized DictCursor that enables to access columns only from keys (aka columns name), whereas DictCursor enables to access data both from keys or index number. I was wondering why RealDictCursor has been implemented if DictCursor offers more flexibility? Is it performance-wise (or memory-wise) so different (in favor of RealDictCursor I imagine...)? In other words, what are RealDictCursor use cases vs DictCursor? 回答1: The main advantage of real

psycopg2: DictCursor vs RealDictCursor

余生长醉 提交于 2020-03-21 11:55:02
问题 AFAIU and from docs, RealDictCursor is a specialized DictCursor that enables to access columns only from keys (aka columns name), whereas DictCursor enables to access data both from keys or index number. I was wondering why RealDictCursor has been implemented if DictCursor offers more flexibility? Is it performance-wise (or memory-wise) so different (in favor of RealDictCursor I imagine...)? In other words, what are RealDictCursor use cases vs DictCursor? 回答1: The main advantage of real

Python psycopg2 not in utf-8

自作多情 提交于 2020-03-18 05:20:05
问题 I use Python to connect to my postgresql data base like this: conn=psycopg2.connect(database="fedour", user="fedpur", password="***", host="127.0.0.1", port="5432") No problem for that. But when I make a query and I want to print the cursor I have something like this: "Fran\xc3\xa7ois" instead of "François" and it cause problem when I want to create a XML document with this. I thkink is come from my encodage, but I found any solution. I try to encode('utf-8') but doesn't work. I have also

psycopg2.OperationalError: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0

蓝咒 提交于 2020-03-15 07:59:26
问题 I'm using Macbook Psycopg2 works well when connecting the localhost db (PostgreSQL on Mac). The error was raised when I tried to connect PostgreSQL db on a Windows10. the following code is what I have for connection, the host is just the IP of the windows10 db= psycopg2.connect(database='dbname',user='username',password="secret",host="192.168.3.9",port="5432") Errors: File "path/to/psycopg2/__init__.py", line 126, in connect conn = _connect(dsn, connection_factory=connection_factory, *