psycopg2

any way to make parameterized queries and encapsulate it python in function

纵饮孤独 提交于 2020-08-10 18:50:32
问题 I want to implement a python function which is going to execute SQL query with parameters. To do so, I started to use psycopg2 for accessing my local db. However, I have written a bunch of very similar SQL queries whereas each SQL statement is slightly different from one to another in terms of taking different values. My goal is I want to write up parametric SQL so I could wrap it up in python function, ideally, I could make function call with arbitrary parameters so it could replace param

psycopg2 use column names instead of column number to get row data

我的梦境 提交于 2020-08-10 05:16:15
问题 So currently when I execute SELECT query and retrieve data I have to get results like this: connection = psycopg2.connect(user="admin", password="admin", host="127.0.0.1", port="5432", database="postgres_db") cursor = connection.cursor() cursor.execute("SELECT * FROM user") users = cursor.fetchall() for row in users: print(row[0]) print(row[1]) print(row[2]) What I want to do is, use column names instead of integers, like this: for row in users: print(row["id"]) print(row["first_name"]) print

Python error: execute cannot be used while an asynchronous query is underway

戏子无情 提交于 2020-07-20 08:32:31
问题 How do I prevent the error “ ProgrammingError: execute cannot be used while an asynchronous query is underway ”? From the docs it says that I should use psycopg2.extras.wait_select if I’m using a coroutine support like gevent., but I’m still get that error when I’m using it. I’ve isolated the error I’m getting in the snippet below. con = psycopg2.connect(database=DATABASE_NAME, user=DATABASE_USERNAME) def execute_query(cur, query, params): psycopg2.extras.wait_select(con) cur.execute(query,

psycopg2 not detected by postgres django

限于喜欢 提交于 2020-07-10 10:32:45
问题 I am using PyCharm on Windows 10 and using Python 3.8 - 64 bit and Django 2.2. pip install psycopg2 successfully installed psycopg2-2.8.5. My database settings are as follows: settings.py, DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'my-database', 'USER': 'my-user', 'PASSWORD': 'my-password', 'HOST': 'localhost', 'PORT': '', } } my-user is the user that I am using and my-password is my password. However, python manage.py makemigrations causes a few

How to setup psycopg2 with Google App Engine PostgreSQL database

时光总嘲笑我的痴心妄想 提交于 2020-07-09 19:56:27
问题 I have an application that is being run on Google's App Engine and I want it to use the associated PostgreSQL database. I'm using psycopg2 to help me with my SQL queries. However I am not sure how I can set up the connection. This is what I currently have: con = psycopg2.connect( host=HOST_NAME, # the IP address of the SQL database database=DATABASE_NAME, # the name of the database (I'm using the default, so this is "postgres" user=USER_NAME, # just the user name that I created password