1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Writing blob from SQLite to file using Python

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am creating a simple script that inserts a binary file into a blob field in a SQLite database:

    import sqlite3
    conn = sqlite3.connect('database.db')
    cursor = conn.cursor()
    input_note = raw_input(_(u'Note: '))
    input_type = 'A'
    input_file = raw_input(_(u'Enter path to file: '))
    with open(input_file, 'rb') as f:
    ablob = f.read()
    f.close()
    cursor.execute("INSERT INTO notes (note, file) VALUES('"+input_note+"', ?)", [buffer(ablob)])
    conn.commit()
    conn.close()


    Now, I need to write a script that grabs the contents of the blob field of a specific record and writes the binary blob to a file. In my case, I use the SQLite database to store .odt documents, so I want to grab and save them as .odt files. How do I go about that?

    Continue reading...

Compartilhe esta Página