Advertisement
AyanUpadhaya

Store images in Sqlite3 DB with Python

Jun 2nd, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. # Save images into Database with python
  2. # Your image should be beside your python script
  3. # Or you can mention the path to your image file
  4. # A database file will be created instantly
  5. # Your image will be save in database
  6. # if you delete your image file
  7. # you can later retrive the image by using this script and the db file
  8. # you have to type image name and id number of image
  9. # Script written by Ayan : ayanu881@gmail.com
  10.  
  11. import sqlite3
  12.  
  13. DATABASE='image.db'
  14.  
  15. conn=sqlite3.connect(DATABASE)
  16. #creating table
  17. def createTable()
  18.     command="""CREATE TABLE objectfiles(photoname TEXT NOT NULL, image BLOB);"""
  19.  
  20.     try:
  21.         conn.execute(command)
  22.         print("Table has been created")
  23.     except:
  24.         print("You already have a safe")
  25.  
  26.  
  27. #converting image file to binary object
  28. def convertToBinary(filename):
  29.     with open(filename,'rb') as f:
  30.         blobData=f.read()
  31.  
  32.     return blobData
  33.  
  34. #inserting binary into DB
  35. def insertFile():
  36.     filename=input("Enter name of photo:")
  37.     params=(filename,convertToBinary(filename))
  38.     conn.execute("INSERT INTO objectfiles VALUES(?,?);",params)
  39.     print("inserted !")
  40.     conn.commit()
  41.  
  42. #show listed files in db
  43. def showfiles():
  44.     command="SELECT rowid,photoname FROM objectfiles;"
  45.  
  46.     cursor=conn.execute(command)
  47.  
  48.     for row in cursor:
  49.         print('ID:',row[0])
  50.         print('PHOTO:',row[1])
  51.         print()
  52.  
  53. #write to hardisk
  54. def writeFile(blobdata,filename):
  55.     with open(filename,'wb') as f:
  56.         f.write(blobdata)
  57.  
  58.     print("Success!")
  59.  
  60. #image retriving function
  61. def retriveImage():
  62.     photo_name=input("Enter photo name:")
  63.     id=int(input("Enter id number:"))
  64.     cursor=conn.execute(f"SELECT image FROM objectfiles WHERE rowid={id};")
  65.     for row in cursor:
  66.         writeFile(row[0],photo_name)
  67.  
  68.  
  69.  
  70. createTable()
  71.  
  72. option=''
  73. while option!='q':
  74.     print("what would like to do")
  75.     print('Press i to store image.')
  76.     print('Press s to show image list.')
  77.     print('Press r to retrive Image.')
  78.     print('Press q to quit')
  79.  
  80.     option=input('>')
  81.  
  82.     if option=='i':
  83.         insertFile()
  84.  
  85.     if option=='s':
  86.         showfiles()
  87.  
  88.     if option=='r':
  89.         retriveImage()
  90.  
  91.     if option=='q':
  92.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement