如何将Python中的二进制转换为Postgresql中的bytea数据类型
所以我有一个图像,我通过如何将Python中的二进制转换为Postgresql中的bytea数据类型,python,python-3.x,postgresql,psycopg2,fastapi,Python,Python 3.x,Postgresql,Psycopg2,Fastapi,所以我有一个图像,我通过.read()读取它的内容,它是UploadFile类的一部分,我相信它是以字节为单位读取的。要将bytes对象转换为bytea数据类型,我使用了psycopg2.Binary() 这是我的代码,用于从图像中读取字节并尝试通过bytea数据类型将其存储在Postgresql中 image\u数组=[] 对于图像中的图像\u上载: contents=wait image.read() 图像\数组.追加(内容) #添加图像验证 对于zip(p、c、t、image_数组)中的(
.read()
读取它的内容,它是UploadFile
类的一部分,我相信它是以字节为单位读取的。要将bytes对象转换为bytea
数据类型,我使用了psycopg2.Binary()
这是我的代码,用于从图像中读取字节并尝试通过bytea
数据类型将其存储在Postgresql中
image\u数组=[]
对于图像中的图像\u上载:
contents=wait image.read()
图像\数组.追加(内容)
#添加图像验证
对于zip(p、c、t、image_数组)中的(权限、文本、特征、图像):
查询=(“插入图像(权限、文本、特征、创建日期、用户id、img)”
f“值({permission},{text},”
f“{characteristics}”
f“,{func.now()},{result[0]['id']}”
f“,”{psycopg2.Binary(image)}'))
等待database.execute(query=query)
这是痕迹
INFO: 127.0.0.1:53168 - "POST /add/?api_key=stringstring&texts=string&characteristics=string&permissions=string HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
await super().__call__(scope, receive, send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/fastapi/routing.py", line 201, in app
raw_response = await run_endpoint_function(
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/fastapi/routing.py", line 148, in run_endpoint_function
return await dependant.call(**values)
File "./app/controllers/add.py", line 49, in add_images
await database.execute(query=query)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/databases/core.py", line 161, in execute
return await connection.execute(query, values)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/databases/core.py", line 263, in execute
return await self._connection.execute(built_query)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/databases/backends/postgres.py", line 188, in execute
query, args, result_columns = self._compile(query)
File "/Users/admin/Library/Caches/pypoetry/virtualenvs/backend-DSfoN2Wn-py3.9/lib/python3.9/site-packages/databases/backends/postgres.py", line 221, in _compile
compiled_query = compiled.string % mapping
KeyError: '$fu'
如果您将完整的错误跟踪作为问题的一部分将更好。添加的错误跟踪您正在将所有内容转换为字符串,请看:您正在使用SyncIO,这是psycopg2不支持的。但在我没有二进制转换的情况下,这是有效的