Skip to content

Instantly share code, notes, and snippets.

@iHTCboy
Created February 2, 2023 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iHTCboy/aa359750df0252b28a709de26e1a7a59 to your computer and use it in GitHub Desktop.
Save iHTCboy/aa359750df0252b28a709de26e1a7a59 to your computer and use it in GitHub Desktop.
sqlite binary data convert to file
#!/usr/bin/env python3
import sqlite3
def write_file(data, filename):
'''Convert binary data and write it on Hard Disk'''
with open(filename, 'wb') as file:
file.write(data)
print(f"Stored blob data into:{filename}\n")
def read_blob(emp_id, audio_path, db_path):
try:
sqlite_con = sqlite3.connect(db_path)
cursor = sqlite_con.cursor()
print("Connected to SQLite")
sql_blob_query = "SELECT * from table where id = ?"
cursor.execute(sql_blob_query, (emp_id,))
record = cursor.fetchall()
for row in record:
name = row[6]
data = row[7]
audio = f"{audio_path}{name}.mp3"
write_file(data, audio)
cursor.close()
except sqlite3.Error as error:
print(f"Failed to read blob data from sqlite table {error}")
finally:
if sqlite_con:
sqlite_con.close()
print("sqlite connection is closed")
if __name__ == '__main__':
db_path = '/Users/HTC/Downloads/audios.sqlite'
audio_path = "/Users/HTC/Downloads/"
read_blob(1, audio_path, db_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment