Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

display image from sql with python

# import module
import mysql.connector
 
#  function to convert data
def convert_data(data, file_name):
    # Convert binary format to images
    # or files data(with given file_name)
    with open(file_name, 'wb') as file:
        file.write(data)
 
 
try:
    # establish connection
    connection = mysql.connector.connect(host='localhost',
                                         database='geeksforgeeks',
                                         user='root',
                                         password='shubhanshu')
    cursor = connection.cursor()
    # getting data by id value
    query = """ SELECT * from demo where id = %s """
 
    id = 1
    cursor.execute(query, (id,))
    records = cursor.fetchall()
    for row in records:
        print("Person Id = ", row[0])
        print("Person Name = ", row[1])
        image = row[2]
        file = row[3]
        # Pass path with filename where we want to save our file
        convert_data(image, "D:GFGimagesOne.png")
        # Pass path with filename where we want to save our file
        convert_data(file, "D:GFGcontent.txt")
 
    print("Successfully Retrieved Values from database")
 
except mysql.connector.Error as error:
    print(format(error))
 
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Comment

PREVIOUS NEXT
Code Example
Python :: set an index to a dataframe from another dataframe 
Python :: time difference between two datetime.time 
Python :: how to reset username and password in django admin 
Python :: python define class 
Python :: is tuple immutable in python 
Python :: python dictonary set default 
Python :: bounding box python 
Python :: python yeild 
Python :: remove multiple elements from a list in python 
Python :: python bubble sort 
Python :: python move and rename files 
Python :: create the dataframe column based on condition 
Python :: Program to Compute LCM 
Python :: python remove specific item from list 
Python :: difference between method and function in pyhon 
Python :: python dict remove duplicates where items are not the same 
Python :: to string python 
Python :: adding number in set in python 
Python :: PY | websocket - server 
Python :: matplotlib savefig legend cut off 
Python :: Python Difference between two dates and times 
Python :: python get env 
Python :: python tkinter code example 
Python :: for enumerate python 
Python :: python pandas read_excel 
Python :: how to update values in tkinter 
Python :: geopandas geometry length 
Python :: square a number in python 
Python :: np.eye 
Python :: random.choices in python 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =