1

DataBase is already created(PostgreSQL)

There is a list:

data = 
['param_1', 0], 
['amp', 0], 
['voltage', 1], 
['params', 1], 
['antenna', 1], 
['freq', 0.00011000000085914508] 

I tried that

import psycopg2
from psycopg2 import sql 

    with psycopg2.connect(dbname='db_name', user='postgres',
                      password='123', host='localhost') as conn:
        conn.autocommit = True
        with conn.cursor() as cur:
            query = "INSERT INTO table_name (%s) VALUES (%s);"
            cur.executemany(query, data)

I need to insert values in the table in the database that contains fields named: 'param_1': 'paRam_2' e.t.c.

How do I generate a query string?

I will be happy for any help, thanks in advance, stack.

3
  • What have you tried so far? Commented Oct 16, 2019 at 16:49
  • 1
    So you want to insert a single row with those numeric values written to the corresponding columns? Commented Oct 16, 2019 at 19:20
  • @GordThompson, yes Commented Oct 16, 2019 at 19:34

2 Answers 2

3

Parameter substitution can only be used to pass column values, not column names, so we'll need to build a list of column names to insert into the SQL command text. Specifically, we'll need to

  1. build a comma-separated string of column names
  2. build a comma-separated string of parameter placeholders
  3. create the INSERT command, including the two items above
  4. create a tuple of (numeric) parameter values
  5. execute the command

That would look something like this:

# create environment
# ~~~~~~~~~~~~~~~~~~
data = (
['param_1', 0],
['amp', 0], 
['voltage', 1], 
['params', 1], 
['antenna', 1], 
['freq', 0.00011000000085914508]
)

# example code
# ~~~~~~~~~~~~
columns = ','.join([f'"{x[0]}"' for x in data])
print(columns)
# "param_1","amp","voltage","params","antenna","freq"

param_placeholders = ','.join(['%s' for x in range(len(data))])
print(param_placeholders)
# %s,%s,%s,%s,%s,%s

sql = f"INSERT INTO table_name ({columns}) VALUES ({param_placeholders})"
print(sql)
# INSERT INTO table_name ("param_1","amp","voltage","params","antenna","freq") VALUES (%s,%s,%s,%s,%s,%s)

param_values = tuple(x[1] for x in data)
print(param_values)
# (0, 0, 1, 1, 1, 0.00011000000085914508)

cur.execute(sql, param_values)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very very much
0

Thank's @GordThompson for his response. It's been very helpful and based on your response I have created two functions: - One for reading string of falues.
- Second to insert values into the table.

I would like to notice that psycopg2 did not fight for a dynamic lookup of the table name in the query string generation

import psycopg2

def insert_to(self, table_name: str, data: dict):
    # формирование строки запроса
    columns = ','.join([f'"{x}"' for x in data])
    param_placeholders = ','.join(['%s' for x in range(len(data))])
    query = f'INSERT INTO "{table_name}" ({columns}) VALUES ({param_placeholders})'
    param_values = tuple(x for x in data.values())
    try:
        self.cur.execute(query, param_values)
    except Exception as e:
        log.exception(f'\r\nException: {e}')
    else:
        log.warning(f'INSERT INTO "{table_name}" {data}')


def read_from(self, table_name: str, dict_for_get_pk: dict) -> any:
    # формирование строки запроса
    columns = ','.join([f'"{x}"' for x in dict_for_get_pk])
    param_placeholders = ','.join(['%s' for x in range(len(dict_for_get_pk))])
    query = f'SELECT * FROM "{table_name}" WHERE ({columns}) = ({param_placeholders})'
    param_values = tuple(x for x in dict_for_get_pk.values())
    try:
        self.cur.execute(query, param_values)
    except Exception as e:
        log.exception(f'\r\nException: {e}')
    else:
        db_values = self.cur.fetchall()
        if db_values:
            return db_values[0]
        else:
            return None

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.