A
A
anya_hacker2021-08-01 15:22:05
PostgreSQL
anya_hacker, 2021-08-01 15:22:05

How to insert bytecode into PostgreSQL?

I am writing a server on Heroku with PostgreSQL.
I want to store an image in a table so that I can send it and save it in the sqlite database in the Android phone (in the sqlite database I specified the blob type).
For a photo in postgresql, I specified the data type bytea.
But after writing the request, an error occurs:
ERROR: invalid input syntax for type bytea
The request itself:

insert into table(name, img) values('toy', bytea('C:\Users\anya\Desktop\games\robot.png'))

the name field is of type text, the img field is of type bytea.
How can an image be stored in bytes? So that you can send it from the server to the device.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2021-08-01
@anya_hacker

First, there is no bytecode in image files. Secondly, it is better not to store images in the database, this has a bad effect on performance. Thirdly, if it’s already pinned down, you should use the appropriate functions to read the file in bytea

G
galaxy, 2021-08-02
@galaxy

The answer generally depends on the UI/client you are using.
For you need to first read the contents of the file into a variable, and then insert it into the request in one way or another.
For example, for PHP you can use pg_escape_bytea + pg_query_params

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question