psycopg2

How to specify Schema in psycopg2 connection method?

十年热恋 提交于 2021-01-01 07:29:20
问题 Using the psycopg2 module to connect to the PostgreSQL database using python. I'm able to execute all of my queries using the below connection method. Now I want to specify a different schema other than the public to execute my SQL statements. Is there any way to specify the schema name in the connection method? conn = psycopg2.connect(host="localhost", port="5432", user="postgres", password="password", database="database", ) I tried to specify schema directly inside the method. schema=

PostgreSQL TypeError: not all arguments converted during string formatting

淺唱寂寞╮ 提交于 2020-12-29 11:59:38
问题 I am executing a query in psycopg2 linked up to a PostgreSQL database. Here is the code in question: with open('dataFile.txt', 'r') as f: lines = f.readlines() newLines = [line[:-1] for line in lines] curr=conn.cursor() lineString = ','.join(newLines) curr.execute("SELECT fields.fieldkey FROM fields LEFT JOIN zone ON zone.fieldkey=fields.fieldkey WHERE zone.zonekey = %s;", (newLines[0])) rows = curr.fetchall() There's no issue connecting to the DB, and the type of lines[0] is definitely

Psycopg2 auto reconnect inside a class

笑着哭i 提交于 2020-12-27 06:52:41
问题 I've got class to connect to my Database. import psycopg2, psycopg2.extensions from parseini import config import pandas as pd, pandas.io.sql as sqlio class MyDatabase: def __init__(self, name='mydb.ini'): self.params = config(filename=name) self.my_connection = psycopg2.connect(**self.params) self.my_cursor = self.my_connection.cursor() def fetch_all_as_df(self, sql_statement): return sqlio.read_sql_query(sql_statement, self.my_connection) def df_to_sql(self, df): table = 'sometable' return

Psycopg2 auto reconnect inside a class

[亡魂溺海] 提交于 2020-12-27 06:51:53
问题 I've got class to connect to my Database. import psycopg2, psycopg2.extensions from parseini import config import pandas as pd, pandas.io.sql as sqlio class MyDatabase: def __init__(self, name='mydb.ini'): self.params = config(filename=name) self.my_connection = psycopg2.connect(**self.params) self.my_cursor = self.my_connection.cursor() def fetch_all_as_df(self, sql_statement): return sqlio.read_sql_query(sql_statement, self.my_connection) def df_to_sql(self, df): table = 'sometable' return

Psycopg2 auto reconnect inside a class

a 夏天 提交于 2020-12-27 06:51:06
问题 I've got class to connect to my Database. import psycopg2, psycopg2.extensions from parseini import config import pandas as pd, pandas.io.sql as sqlio class MyDatabase: def __init__(self, name='mydb.ini'): self.params = config(filename=name) self.my_connection = psycopg2.connect(**self.params) self.my_cursor = self.my_connection.cursor() def fetch_all_as_df(self, sql_statement): return sqlio.read_sql_query(sql_statement, self.my_connection) def df_to_sql(self, df): table = 'sometable' return

Psycopg2 - not all arguments converted during string formatting

。_饼干妹妹 提交于 2020-12-25 10:09:14
问题 I'm trying to insert the value of the variable test_text into a Postgres 9.6 database, each time the database_insert function is triggered. I'm using Python 3.6 and psycopg2 v 2.7 If I use the below code without the placeholder: e.g replace %s with 'test' and remove , (test_text) - it works as I would expect... def database_insert(update): test_text = 'This is some test text' with psycopg2.connect("DB CONNECTION DETAILS ARE HERE'") as conn: cur = conn.cursor() cur.execute("INSERT INTO test

Psycopg2 - not all arguments converted during string formatting

早过忘川 提交于 2020-12-25 10:05:19
问题 I'm trying to insert the value of the variable test_text into a Postgres 9.6 database, each time the database_insert function is triggered. I'm using Python 3.6 and psycopg2 v 2.7 If I use the below code without the placeholder: e.g replace %s with 'test' and remove , (test_text) - it works as I would expect... def database_insert(update): test_text = 'This is some test text' with psycopg2.connect("DB CONNECTION DETAILS ARE HERE'") as conn: cur = conn.cursor() cur.execute("INSERT INTO test

Psycopg2 - not all arguments converted during string formatting

可紊 提交于 2020-12-25 10:04:24
问题 I'm trying to insert the value of the variable test_text into a Postgres 9.6 database, each time the database_insert function is triggered. I'm using Python 3.6 and psycopg2 v 2.7 If I use the below code without the placeholder: e.g replace %s with 'test' and remove , (test_text) - it works as I would expect... def database_insert(update): test_text = 'This is some test text' with psycopg2.connect("DB CONNECTION DETAILS ARE HERE'") as conn: cur = conn.cursor() cur.execute("INSERT INTO test

Why are Pandas and GeoPandas able to read a database table using a psycopg2 connection but have to rely on SQLAlchemy to write one?

孤街醉人 提交于 2020-12-25 01:30:53
问题 Context I just get into trouble while trying to do some I/O operations on some databases from a Python3 script. When I want to connect to a database, I habitually use psycopg2 in order to handle the connections and cursors. My data are usually stored as Pandas DataFrames and/or GeoPandas's equivalent GeoDataFrames. Difficulties In order to read data from a database table; Using Pandas: I can rely on its .read_sql() methods which takes as a parameter con , as stated in the doc: con :

Why are Pandas and GeoPandas able to read a database table using a psycopg2 connection but have to rely on SQLAlchemy to write one?

点点圈 提交于 2020-12-25 01:29:50
问题 Context I just get into trouble while trying to do some I/O operations on some databases from a Python3 script. When I want to connect to a database, I habitually use psycopg2 in order to handle the connections and cursors. My data are usually stored as Pandas DataFrames and/or GeoPandas's equivalent GeoDataFrames. Difficulties In order to read data from a database table; Using Pandas: I can rely on its .read_sql() methods which takes as a parameter con , as stated in the doc: con :