psycopg2

multi thread python psycopg2

僤鯓⒐⒋嵵緔 提交于 2021-02-08 05:46:40
问题 I'm using multi thread inside my program in python. I've got 3 queues. In one of them I'm inserting data to postgres database. But before, I need to check if in the database already exists row with specific domain name. So I've got: class AnotherThread(threading.Thread): def __init__(self, another_queue): threading.Thread.__init__(self) self.another_queue = another_queue def run(self): while True: chunk = self.another_queue.get() if chunk is not '': dane = chunk[0].split(',',2) cur.execute(

sqlalchemy with postgres: insert into a table whose columns have parentheses

时光总嘲笑我的痴心妄想 提交于 2021-02-07 08:01:36
问题 Suppose you have a table "foo" in postgres with column name "col (parens) name" . The psql command INSERT INTO "foo" ("col (parens) name") VALUES ('bar'); works just fine. However, if I try to do the same using sqlalchemy (version 0.9.7), the resulting python code fails: conn = sqlalchemy.create_engine('postgresql://name:password@host:port/database') meta = sqlalchemy.schema.MetaData() meta.reflect(bind=conn) foo = meta.tables['foo'] vals = [{'col (parens) name': 'hi'}, {'col (parens) name':

psycopg2: Update multiple rows in a table with values from a tuple of tuples

蓝咒 提交于 2021-02-07 02:00:53
问题 I'm attempting to update several rows at once using a tuple of tuples. I figured out how to construct the sql statement from this post, but implementing it in psycopg2 has proven to be more challenging. Here's what I have: c = db.cursor() new_values = (("Richard",29),("Ronald",30)) sql = """UPDATE my_table AS t SET name = e.name FROM (VALUES %s) AS e(name, id) WHERE e.id = t.id;""" c.execute(sql, (new_values,)) The result is an error: ProgrammingError: table "e" has 1 columns available but 2

pyscopg2: Is it possible to dynamically add %s in loop

坚强是说给别人听的谎言 提交于 2021-02-05 11:23:26
问题 I am trying to create a method in python insert records into a table passing in a list of column names, and an associated list of records. I was able to set it up where the column names populated dynamically via a for loop, but I can't figure out how to do the same thing with values because the psycopg2.executemany function relies on having %s's as placeholders. Is it possible to have the number of %s's in the string populate dynamically via a loop? Is there another way to do this? def load

Pandas transpose rows to columns

£可爱£侵袭症+ 提交于 2021-02-04 17:55:26
问题 I am writing the result of an sql query into an excel sheet and attempting to transpose rows into columns but cannot seem to get Pandas to budge, there seems to be an conundrum of some sort with excel. I have done my googlefu and have looked at: how to switch columns rows in a pandas dataframe How to convert rows values in dataframe to columns labels in Python after groupby? Convert row to column in Python Pandas Python Pandas: Convert Rows as Column headers None seems to work. Please advise

Pandas transpose rows to columns

和自甴很熟 提交于 2021-02-04 17:55:07
问题 I am writing the result of an sql query into an excel sheet and attempting to transpose rows into columns but cannot seem to get Pandas to budge, there seems to be an conundrum of some sort with excel. I have done my googlefu and have looked at: how to switch columns rows in a pandas dataframe How to convert rows values in dataframe to columns labels in Python after groupby? Convert row to column in Python Pandas Python Pandas: Convert Rows as Column headers None seems to work. Please advise

Psycopg2 db connection hangs on lost network connection

痞子三分冷 提交于 2021-02-04 17:10:02
问题 Problem description I'm using psycopg2 to connect to my PostgreSQL database on a remote host. I open a connection and wait for requests, then for each request I run queries on the connection and return data. But when the network connection is lost after the connection is already open the next db query hangs and I have to kill the program manually. Details: it hangs for at least 2 hours (I couldn't wait longer) the "network down" situation is actually VPN going down (db host is accesible only

Psycopg2 db connection hangs on lost network connection

梦想与她 提交于 2021-02-04 17:06:34
问题 Problem description I'm using psycopg2 to connect to my PostgreSQL database on a remote host. I open a connection and wait for requests, then for each request I run queries on the connection and return data. But when the network connection is lost after the connection is already open the next db query hangs and I have to kill the program manually. Details: it hangs for at least 2 hours (I couldn't wait longer) the "network down" situation is actually VPN going down (db host is accesible only

Psycopg2 db connection hangs on lost network connection

巧了我就是萌 提交于 2021-02-04 17:05:42
问题 Problem description I'm using psycopg2 to connect to my PostgreSQL database on a remote host. I open a connection and wait for requests, then for each request I run queries on the connection and return data. But when the network connection is lost after the connection is already open the next db query hangs and I have to kill the program manually. Details: it hangs for at least 2 hours (I couldn't wait longer) the "network down" situation is actually VPN going down (db host is accesible only

Psycopg2 can not create table

北战南征 提交于 2021-01-29 19:36:49
问题 From Jupiter notebook, I was able to create Database with Psycopg2. But somehow I was not able to create Table and store element in it. import psycopg2 from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT con = psycopg2.connect("user=postgres password='abc'"); con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT); cursor = con.cursor(); name_Database = "socialmedia"; sqlCreateDatabase = "create database "+name_Database+";" cursor.execute(sqlCreateDatabase); With the above code, I can see