【问题标题】:how to append a bytes in a Postgres bytea array如何在 Postgres bytea 数组中附加一个字节
【发布时间】:2021-11-03 22:47:30
【问题描述】:

我有一个帐户表数据库,其中存储用户名、电子邮件、密码和图像[]。每次他们共享图片时,我想要做的是图片字节应附加在图像类型 bytea [] 中。用了好几种方法都报错,怎么解决?


bytea = []

my_cursor = mydb.cursor()

with open(os.path.join('image\\Image.png'), 'rb') as file:
    image = file.read()
    #bytea.append(image)

insert_info = f"SELECT array_append(images, {image}) WHERE id = 7"


my_cursor.execute(insert_info)
mydb.commit()

错误

psycopg2.errors.SyntaxError: syntax error at or near "("
LINE 1: SELECT account array_append(images, b'\x89PNG\r\n\x1a\n\x00\...

【问题讨论】:

  • 你是SELECTing 不是INSERTing 或UPDATEing;这个 >>>insert_info = f"SELECT array_append(images, {image}) WHERE id = 7" 应该是这个 >>>insert_info = f"UPDATE array_append(images, {image}) WHERE id = 7"
  • 它给我一个错误psycopg2.errors.SyntaxError: syntax error at or near "("

标签: python arrays postgresql append bytea


【解决方案1】:

你必须写选择而不是插入:

bytea = []

my_cursor = mydb.cursor()

with open(os.path.join('image\\Image.png'), 'rb') as file:
    image = file.read()
    bytea.append(image)

insert_info = f"insert array_append(images, {image}) WHERE id = 7"


my_cursor.execute(insert_info)
mydb.commit()

【讨论】:

  • 我使用了上面的脚本,但仍然出现错误psycopg2.errors.SyntaxError: syntax error at or near "array_append" LINE 1: INSERT array_append(images, 'b'\x89PNG\r\n\x1a\n\x00\x00\x00...
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 2018-02-05
  • 2013-09-04
  • 1970-01-01
相关资源
最近更新 更多