Home > OS >  How do I store file names in a database in python?
How do I store file names in a database in python?

Time:08-24

I am trying to get a list of files in a user specified directory to be saved to a database. What I have at the moment is :

import os
import sqlite3

def get_list():
    folder = input("Directory to scan : ")
    results = []
    for path in os.listdir(folder):
        if os.path.isfile(os.path.join(folder, path)):
            results.append(path)
    print(results)
    return results


def populate(results):
    connection = sqlite3.connect("videos.db")
    with connection:
        connection.execute("CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY, file_name TEXT);")
    for filename in results:
        insert_string = "INSERT INTO files (file_name) VALUES ('" filename "');"
         connection.execute(insert_string)


filelist = get_list()
populate(filelist)

It runs without a problem and prints out a list of the file names, which is great, but then when it's running the INSERT SQL statement, that seems to have no effect on the database table. I have tried to debug it, and the statement which is saved in the variable looks good, and when executing it manually in the console, it inserts a row in the table, but when running it, nothing changes. Am I missing something really simple here ?

CodePudding user response:

Python's SQLite3 module doesn't auto-commit by default, so you need to call connection.commit() after you've finished executing queries. This is covered in the tutorial.

In addition, use ? placeholders to avoid SQL injection issues:

cur.execute('INSERT INTO files (file_name) VALUES (?)', (filename,))

Once you do that, you can insert all of your filenames at once using executemany:

cur.executemany(
    'INSERT INTO files (file_name) VALUES (?)',
    [(filename,) for filename in results],
)
  • Related