MagicHat
MagicHat

Reputation: 429

Insert on mysql from filesystem

I need to insert the name of files from specif folder on my database.

I use Debian, and i have a folder with some images:

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

How can i take this images like string and insert in specif mysql table?

I have no ideia, and search for this not a ready script.

Any tip?

Upvotes: 0

Views: 49

Answers (1)

Pedram A
Pedram A

Reputation: 187

You can use python

here's an example:

import mysql.connector
import os

directory = '/home/usr/folder_img'

conn = mysql.connector.connect(user='test', password='test',
                               host='127.0.0.1',
                               database='images')
cursor = conn.cursor()

for filename in os.listdir(directory):
    query = ("INSERT INTO images (imagename) VALUES (%(image_name)s)")
    data = {'image_name': filename}
    cursor.execute(query, data)

conn.commit()
cursor.close()
conn.close()

Upvotes: 1

Related Questions