I have a CRUD form with entries and 4 button for deleting, updating, creating, getting values from my database, I want to implement another button to open an imagen which is binded to my id entry also able to works with my deleting, updating, creating, getting buttons, I've been trying to use BLOB and I'm able to save an image in my database as BLOB. Actually I understand that I need to create textvariables for my entries like 'idvar = StringVar() , namevar = Stringvar()..., etc', So I'm not sure how to do it for an image label in order to work with my CRUD buttons deleting, updating, creating, getting
This is my code I got so far and it's working well saving images into my photos columns:
from tkinter import *
import sqlite3
top = Tk()
top.configure(width='444', heigh='400')
conn = sqlite3.connect('test.db')
c = conn.cursor()
def enterdata():
id = 'hello'
photo = convert_pic()
c.execute('INSERT INTO test (id, photo) VALUES (?, ?)', (id, photo)) #Here my id has integer value and my photo has BLOB value in my database
conn.commit()
def convert_pic():
filename = 'images/image6.jpg'
with open(filename, 'rb') as file:
photo = file.read()
return photo
btn = Button(top, text='save photo', command=enterdata)
btn.place(x='100', y='111')
mainloop()
Now that you have the BLOB you can use io.BytesIO. I will create an example to demonstrate, like:
from PIL import Image, ImageTk
from io import BytesIO
def show(data):
img_byte = BytesIO(data)
img = ImageTk.PhotoImage(Image.open(img_byte))
Label(root,image=img).pack()
root.image = img # Keep a reference
So now you can query the database and fetch the value:
def fetch():
c = con.cursor()
id = 1 # Any id
c.execute('SELECT photo FROM test where id=?',(id,))
data = c.fetchall()[0][0] # Get the blob data
show(data) # Call the function with the passes data
This will show the image in a label in the root window for the entered id.
I am trying to update a database from a csv file. I am able to connect to the database, run through the lines that are in the csv, compose the desired SQL line, but for some reason it never hits the Postgresql DB. Console and pycharm show no errors.
Here is the .py
import csv
import psycopg2
filename = 'C:\dev\student_feedback\student_feedback\student_feedback.csv'
fields = []
rows = []
host='localhost'
port=5432
database='ipfeedback'
user='postgres'
password='#########'
def import_student_survey_csv():
print('Maybe Updating surveys...')
def doQuery(connection):
cur = connection.cursor()
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
for row in csvreader:
rows.append(row)
nrows = rows.pop()
qry = (f"INSERT INTO ipfeedbackdb_studentsurvey ({fields[0]},{fields[1]},{fields[2]},{fields[3]},{fields[4]},{fields[5]},{fields[6]},{fields[7]},{fields[8]},{fields[9]},{fields[10]},{fields[11]}) VALUES ({nrows[0]},{nrows[1]},{nrows[2]},{nrows[3]},{nrows[4]},{nrows[5]},{nrows[6]},{nrows[7]},{nrows[8]},'{nrows[9]}','{nrows[10]}','{nrows[11]}');")
print('QRY', qry)
cur.execute(qry)
csvfile.close()
myConnection = psycopg2.connect(host=host, port=port, user=user, password=password, dbname=database)
doQuery(myConnection)
myConnection.close()
import_student_survey_csv()
the .csv
added,spid,q1,q2,q3,q4,q5,q6,q7,best_part,improve,comments
21021,0,1,1,1,1,1,1,1,"qwe","qwe","qwe"
21021,0,1,1,1,1,1,1,1,"asd","asd","asd"
21021,0,1,1,1,1,1,1,1,"x","x","x"
21021,12345,1,1,1,1,1,1,1,"x","x","x"
the table data
the db structure
I am not able to insert data to my MySQL db.
getting the below message. Please suggest.
cursor.execute("""INSERT INTO dht11serial (humidity,temperature) VALUES (%s,%s)""", 70.0,32.0)
It gives the below:
<generator object MySQLCursor._execute_iter at 0x03A62D10>
FYI i am using python 3.8.3
Please find the entire script:
import serial
import MySQLdb
import time
dbConn = MySQLdb.connect("localhost","root","","dbserial") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()
device = 'COM18' #this will have to be changed to the serial port you are using
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
except:
print "Failed to connect on",device
while True:
try:
time.sleep(2)
data = arduino.readline() #read the data from the arduino
print data
pieces = data.split(" ") #split the data by the tab
#Here we are going to insert the data into the Database
try:
cursor.execute("INSERT INTO dht11serial (humidity,temperature) VALUES (%s,%s)", (pieces[0],pieces[1]))
dbConn.commit() #commit the insert
cursor.close() #close the cursor
except MySQLdb.IntegrityError:
print "failed to insert data"
finally:
cursor.close() #close just incase it failed
except:
print "Failed to get data from Arduino!"
How to read CSV file in mysql databse using Django.
I want to read csv file data using django and write this csv data in MySQL Databases.
Take a look at django-import-export, which covers exactly this use case.
here is the solution whatever i understand from your question
First of all create browse button for user to take CSV file as a input and then store it temprory in apps's static/upload folder.Then pass the path of that file to our datastore function present in views.py.
forms.py
from django import forms
class browse(forms.Form):
file = forms.FileField()
views.py
import csv
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
def index(request):
result = None
form = None
if request.method == 'POST':
form = browse(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file']) # store file in upload folder
path = "static/upload/"+ str(request.FILES['file'])#path of selected file
with open(path,'rt')as f:
data = csv.reader(f)
for row in data:
storedata(row)
else:
pass
def storedata(row):
connection = mysql.connector.connect(host='localhost',
database='electronics',
user='root',
password='root')
mySql_insert_query = """INSERT INTO Laptop (name, fname, lname)
VALUES
(row[0], row[1], row[2]) """
cursor = connection.cursor()
cursor.execute(mySql_insert_query)
connection.commit()
print(cursor.rowcount, "Record inserted successfully into Laptop table")
cursor.close()
models.py
from django.db import models
def handle_uploaded_file(f):
with open('static/upload/'+f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
I have a CSV file. It contains the product ID, product category, and the URL of the product image.
This is the code I tried in the google colab.
import csv
import request
with open('product.csv') as csvfile:
csvrows = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvrows:
_product_id = row[0]
catagory = row[1]
image_url = row[2]
print(image_url)
result = requests.get(image_url, stream=True)
if result.status_code == 200:
image = result.raw.read()
open(product.csv,"wb").write(image)
I need to download all the information with product image in google colab for further steps.
Invalid syntax Image_url. Did you mean http:\\image_url?