Rilcon42
Rilcon42

Reputation: 9763

Postgresql insert multiple columns malformed array literal

I am following a tutorial from here, and trying to adapt it to pass multiple column values. I've tried solving this by researching the error message but every time I've tried re-writing the values with a { I get a different set of errors. What am I doing wrong?

DataError: malformed array literal: "value1" LINE 1: ... public."Texas"(licensenum,businessowner) VALUES ('value1','... ^ DETAIL: Array value must start with "{" or dimension information.

My Code:

import psycopg2
conn = psycopg2.connect(host="localhost",database="postgres", user="postgres", password="supershinypwdhere")
cur = conn.cursor()
cur.executemany("insert into public.\"Texas\"(licensenum,businessowner) VALUES (%s,%s)", [('value1','value2'),('value3','value4')])
conn.commit()
cur.close()
conn.close()

Upvotes: 1

Views: 2499

Answers (1)

klin
klin

Reputation: 121604

The error message means that the columns licensenum and businessowner (or one of them) are arrays. Probably they should be simple text, then your program would work well. However, if you really want them to be arrays, then you should pass lists, not strings as arguments, e.g.:

cur.executemany(
    "insert into public.\"Texas\"(licensenum,businessowner) VALUES (%s,%s)", 
    [(['value1'],['value2']),(['value3'],['value4'])])

Upvotes: 1

Related Questions