数组值必须以" {"或维度信息

时间:2018-04-04 22:36:37

标签: python sql postgresql

这是我的创建表代码:

cur.execute('CREATE TABLE IF NOT EXISTS Customers (id INTEGER PRIMARY KEY, firstName Varchar(30), lastName Varchar(30), street TEXT, city Varchar(50), state Varchar(2), zip INTEGER);')

这是我的Upsert功能:

def upsert_customer(customer):
    cur=conn.cursor()

    if 'id' in customer:
        cur.execute('UPDATE Customers SET firstName= %s, lastName = %s, street=%s, city=%s, state=%s, zip=%s WHERE id =%s;' , (customer['firstName'], customer['lastName'], customer['street'], customer['city'], customer['state'], customer['zip']))
    else:
        cur.execute('INSERT INTO Customers( firstName, lastName, street,city,state,zip) VALUES ( %s, %s, %s, %s, %s, %s);' , (customer['firstName'], customer['lastName'], customer['street'], customer['city'], customer['state'], customer['zip']))

    conn.commit()

当我从网页添加客户时,我收到错误消息,并在网页中显示以下错误:"内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。"

该程序将完成将网页连接到ElephantSQL中的PostgreSQL的功能的实现。我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的表定义与声明的错误无法协调 - 您没有数组,但您看到的错误显然来自错误的数组定义,例如:

t=# select 'not array'::text[];
ERROR:  malformed array literal: "not array"
LINE 1: select 'not array'::text[];
               ^
DETAIL:  Array value must start with "{" or dimension information.