0

I want to insert the same decimal value into multiple columns in the same PostgreSQL table using this python script to generate some testing data

 def insert_payout(payout):

     vals = [payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout,
        payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout,
        payout, payout, payout, payout, payout, payout, payout, payout] #works

       sql = '''INSERT INTO test_db.public.test_node
       (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32)
  VALUES
       (val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val, val ) '''

  conn = None
 
     print(vals)#remove after testing
     try:
         # read database configuration
         params = setparams()#managed elsewhere just connects to the db working
         # connect to the PostgreSQL database
         conn = psycopg2.connect(**params)
         # create a new cursor
         cur = conn.cursor()
         # execute the INSERT statement
         cur.execute(sql, vals)
         # commit the changes to the database
         conn.commit()
         # close communication with the database
         cur.close()
      except (Exception, psycopg2.DatabaseError) as error:
          print(error)
      finally:
          if conn is not None:
              conn.close()

    if __name__ == '__main__':
         insert_payout(3.3)

The error I am getting is this:

  [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
   not all arguments converted during string formatting 

Casting to a decimal doesn't work and I reworked it to int and that doesn't work either.

6
  • 1
    This (val, val, val, val,...) needs to be (%s , %s, %s, %s...) per Parameters. @MikeOrganek it just needs to be a sequence of values, list or tuple will work. Commented Aug 11, 2022 at 16:20
  • @AdrianKlaver I listed it using vals = [payout, etc] isnt that a list? Commented Aug 11, 2022 at 16:27
  • 1
    vals = [payout, payout, ...] is not the problem, it is the other end (val, val...) needs to be (%s, %s...). You could simplify further by using named arguments where vals = {"val": payout} and (%(val)s, %(val)s, %(val)s ...). Then you would not have to build a list of inputs just the dict and the payout value would be picked up by %(val)s. Commented Aug 11, 2022 at 16:34
  • 1
    I believe you only have 31 val but 32 columns and 32 items in your list (unless I'm mistaken). That will likely be the next error to pop. Commented Aug 11, 2022 at 17:49
  • @JNevill good catch! Commented Aug 11, 2022 at 18:48

1 Answer 1

1

Change to this:

def insert_payout(payout):

     vals = [payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout,
        payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout, payout,
        payout, payout, payout, payout, payout, payout, payout, payout] #works

       sql = '''INSERT INTO test_db.public.test_node
       (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32)
  VALUES
       (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) '''

  conn = None
 
     print(vals)#remove after testing
     try:
         # read database configuration
         params = setparams()#managed elsewhere just connects to the db working
         # connect to the PostgreSQL database
         conn = psycopg2.connect(**params)
         # create a new cursor
         cur = conn.cursor()
         # execute the INSERT statement
         cur.execute(sql, vals)
         # commit the changes to the database
         conn.commit()
         # close communication with the database
         cur.close()
      except (Exception, psycopg2.DatabaseError) as error:
          print(error)
      finally:
          if conn is not None:
              conn.close()

    if __name__ == '__main__':
         insert_payout(3.3)

An alternate form, using named arguments, would be:

vals = {"val": payout}
sql = '''INSERT INTO test_db.public.test_node
       (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32)
  VALUES
      (%(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, 
%(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, 
%(val)s,%(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s, %(val)s)'''
Sign up to request clarification or add additional context in comments.

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.