Insert file system image name in mysql

0

I use Debian, and I need to capture the name of the images in a folder and insert the names (as string) into a mysql table.

ss:~/folder_img$
859034809583_img.jpg
458389547389_img.jpg
...

This problem has now come up and I have no idea how to solve it.

I'm not looking for a script, just a light.

    
asked by anonymous 04.07.2018 / 06:36

2 answers

1

One way would be to use a bash script that mounts the INSERT commands and passes as input to the mysql client, for example:

ls ~/folder_img \
| xargs -I{} echo "INSERT INTO imagens (arquivo) VALUES ('{}');" \
| mysql -u [usuario] -p [senha] 

If such a load is recurring, such as a process scheduled via crontab , it is interesting to pass the password per parameter or to use another form of automatic authentication (# password.

    
04.07.2018 / 15:58
1

Assuming your image files are contained in a single directory:

$ ls -al ./folder_img/*.png 
-rw-rw-r-- 1 lacobus lacobus  349237 Jul  4 18:03 ./folder_img/alpha.png
-rw-rw-r-- 1 lacobus lacobus  312568 Jul  4 18:03 ./folder_img/beta.png
-rw-rw-r-- 1 lacobus lacobus  159315 Jul  4 18:03 ./folder_img/delta.png
-rw-rw-r-- 1 lacobus lacobus 2453952 Jul  4 18:03 ./folder_img/episilon.png
-rw-rw-r-- 1 lacobus lacobus  482526 Jul  4 18:03 ./folder_img/gamma.png
-rw-rw-r-- 1 lacobus lacobus  848590 Jul  4 18:03 ./folder_img/omega.pn

You can use the find utility to generate an SQL script from the image files contained in this directory, in a non-recursive way. The following example writes a SQL script to the file script.sql , see:

$ find ./folder_img -maxdepth 1 -iname "*.png" -type f -exec \
echo "INSERT INTO tb_imagem (nome_arquivo) VALUES ('{}');" \; > script.sql

Output (% with%):

INSERT INTO tb_imagem (nome_arquivo) VALUES ('./folder_img/delta.png');
INSERT INTO tb_imagem (nome_arquivo) VALUES ('./folder_img/alpha.png');
INSERT INTO tb_imagem (nome_arquivo) VALUES ('./folder_img/gamma.png');
INSERT INTO tb_imagem (nome_arquivo) VALUES ('./folder_img/beta.png');
INSERT INTO tb_imagem (nome_arquivo) VALUES ('./folder_img/episilon.png');
INSERT INTO tb_imagem (nome_arquivo) VALUES ('./folder_img/omega.png');

To run the generated script in your database:

$ mysql -h [servidor] -u [usuario] -p [senha] -D [database] < script.sql 
    
04.07.2018 / 23:21