I´m quiet new to Python and trying my first own projects. At the moment I´m struggeling with an SQL insert of a dataframe to a Postgres database with placeholders for the values. I have one Python Script (database_config) which creates the database object and also the methods like execute(), which I want to call in my main script.
When I try to do the insert with an database connection/cursor which is generated in the main script it works well. But when I try to call the execute() method from the database_config script with the same query-string I get the following error message:
"FEHLER: Syntaxfehler bei »%« LINE 19: VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, ..."
The database-config looks like:
import os
import psycopg2
# password for the database from an environment variable
db_pw = os.environ.get('DB_PASS')
conn_str = "host=localhost user=postgres dbname=nfl_scores_bets password={}".format(db_pw)
class MyDatabase():
def __init__(self):
self.conn = psycopg2.connect(conn_str)
self.cur = self.conn.cursor()
self.conn.set_session(autocommit=True)
def query_func(self, query, params=None):
try:
self.cur.execute(query)
except psycopg2.Error as e:
print(e)
The insert string and the method call from the main script are:
scores_bets_table_insert = ("""INSERT INTO scores_bets (
schedule_date,
schedule_season,
schedule_week,
schedule_playoff,
team_home,
score_home,
score_away,
team_away,
team_favorite_id,
spread_favorite,
over_under_line,
stadium_name,
stadium_neutral,
weather_temperature,
weather_wind_mph,
weather_humidity,
weather_detail)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""")
# works fine, if I create the datbase connection in this main script
for i, row in scores_bets.iterrows():
cur.execute(scores_bets_table_insert, list(row))
# doesn´t work when I do the insert with the method from the database_config script
db =MyDatabase()
for i, row in scores_bets.iterrows():
db.query_func(scores_bets_table_insert, list(row))
Till now I couldn´t figured out what creates the syntax-error or what is the diffenrence between executing the same query string with the ursor from the same script or calling the execute() method from the other script. I couldn´t find the reason in the psycopg2 documentation, hopefully some one of you see the error or can give me a hint what I'm doing wrong.
?instead of%s?query_funcuseparams??results in the same error. What is the difference between%sand?? in the most docs, tutorials the placeholder%swas used. I`m not sure what was my Intention with params = None.psycopg2you can use either use%sor the named version%(some_name)sper Parameter passing. Inquery_func()you needself.cur.execute(query, params)in order to get thelist(row)parameters passed into the query for use by the placeholders%s. You are getting the syntax error becausepsycopg2has nothing to match with%sso it passing the query through as as Postgres does not know what to do with%s.paramsto the execute() Method it works well.