I need to put data from the SQL query into a Pandas dataframe. Please tell me is it possible to get column names the query results? I found that there is a keys() function in sqlalchemy for that but it does not work for me:
import mysql.connector
import pandas as pd
mydb = mysql.connector.connect(
host="SQLServer",
user="sqlusr",
password="usrpasswd",
database="sqldb"
)
cursor = mydb.cursor()
Query="SELECT Title, Review, Rate FROM reviews;"
cursor.execute(Query)
df = pd.DataFrame(cursor.fetchall())
df.columns = cursor.keys()
AttributeError: 'CMySQLCursor' object has no attribute 'keys'
I think that it your are searching for
cnx = mysql.connector.connect(user=DB_USER, password=DB_USER_PASSWORD, host=DB_HOST, database=DB_NAME)
cursor = cnx.cursor()
query = ("SELECT `name`, `ftp_name`, `created_at`, `status` AS `status_customer` FROM `customers"
"WHERE `status` = %(status)s")
cursor.execute(query, { 'status': 1 })
# cursor.description will give you a tuple of tuples where [0] for each is the column header.
num_fields = len(cursor.description)
field_names = [i[0] for i in cursor.description]
print(num_fields)
print(field_names)
>>> 4
>>> [u'name', u'ftp_name', 'created_at', u'status_customer']
# OR just use this cursor function:
print(cursor.column_names)
>>> (u'name', u'ftp_name', 'created_at', u'status_customer')
Hope this helps!
SHOW COLUMNS FROM your-database-name.your-table-name
Related
conn = mysql.connector.connect(
host="localhost",
user="root",
passwd="12123123412"
database='newdb')
cur = conn.cursor()
xx_zz = self.screen.get_screen('end').ids["rgr"].text
ee_zz = self.screen.get_screen('end').ids["gfd"].text
qur = f"SELECT * FROM (%s) WHERE bedrooms = '(%s)' "
val = (xx_zz, ee_zz)
cur.execute(qur, val)
records = cur.fetchall()
I suggest that we use a function to create the query string using match-case. This will avoid any risk of SQL injection as we are not using the string provided by the front end.
You will need to modify and complete the option values and table names and decide whether there should be a default table name or no result if the option provided is not found.
Obviously this code has not been tested.
def makeQuery( option ):
match option:
case 'option1':
return f"SELECT * FROM table_name_1 WHERE bedrooms = '(%s)' "
case 'option2':
return f"SELECT * FROM table_name_2 WHERE bedrooms = '(%s)' "
case _:
return f"SELECT * FROM default_table_name WHERE bedrooms = '(%s)' "
conn = mysql.connector.connect(
host="localhost",
user="root",
passwd="12123123412"
database='newdb')
cur = conn.cursor()
xx_zz = self.screen.get_screen('end').ids["rgr"].text
ee_zz = self.screen.get_screen('end').ids["gfd"].text
qur = makeQuery(xx_zz )
val = ( ee_zz )
cur.execute(qur, val)
records = cur.fetchall()
In textInput (field) you use hint_text to show a placeholder in a text field(input).
I'm trying to read the coordinates from a MySQL database with Python and reproduce them on a map with Folium.
But I noticed that only the last of the 43 records are output and entered into the map and I don't know why.
I have the feeling that I can't see the wood for the trees anymore.
Maybe you can help me with how to read out all 43 data sets?
I have the complete code below including a screenshot of what this code outputs:
import folium
import mysql
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="firmen"
)
if(mydb):
print("Verbindung erfolgreich")
else:
print("Verbindung fehlgeschlagen")
cursor = mydb.cursor()
cursor.execute("SELECT * from leipzig")
result = cursor.fetchall()
cursor.close()
#13 = Longitude and 12 = Latitude
for data in result:
ID = data[0]
name = data[1]
lon = data[13]
lat = data[12]
mydb.close()
print("Verbindung geschlossen")
# Create a Map instance
mymap = folium.Map(location=[51.268360, 12.419357], tiles='stamentoner',
zoom_start=10, control_scale=True)
tooltipMeta = ID, name
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
folium.TileLayer('stamenwatercolor').add_to(mymap)
folium.LayerControl().add_to(mymap)
# Display the map
mymap
#sentence & #borisdonchev are right, the part where
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
should become
for data in result:
ID = data[0]
name = data[1]
lon = data[13]
lat = data[12]
tooltipMeta = ID, name
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
I first had to create the map and then access the database.
So I managed to create the markers inside the loop
import folium
import mysql
import mysql.connector
mymap = folium.Map(location=[51.268360, 12.419357], tiles='stamentoner',
zoom_start=10, control_scale=True)
folium.TileLayer('stamenwatercolor').add_to(mymap)
folium.LayerControl().add_to(mymap)
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="firmen"
)
if(mydb):
print("Verbindung erfolgreich")
else:
print("Verbindung fehlgeschlagen")
cursor = mydb.cursor()
cursor.execute("SELECT * from leipzig")
result = cursor.fetchall()
cursor.close()
mydb.close()
print("Verbindung geschlossen")
#13 = Longitude and 12 = Latitude
for data in result:
ID = data[0]
name = data[1]
lon = data[13]
lat = data[12]
tooltipMeta = ID, name
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
# Display the map
mymap
I am using mysql.connector to query values from the database in python.
I need to write a select query with "IN" in the where clause.
I tried to pass the values as a list, tuple, etc.
But it is throwing the error that the "Python 'data-type' cannot be converted to a MySQL type"
import mysql.connector
from mysql.connector import Error
analyticsdb = mysql.connector.connect(
pool_name = 'analyticspool',
pool_size = 10,
host="localhost",
user="root",
passwd="",
database="analytics"
)
analyticsCursor = analyticsdb.cursor(dictionary=True)
gender_values = ['Male', 'Female']
registeredQuery = "SELECT COUNT(DISTINCT uid) AS registered FROM `users` WHERE gender IN (%s)"
placeholders = (gender_values)
analyticsCursor.execute(registeredQuery, placeholders)
regCount = analyticsCursor.fetchone()
According to the documentation here https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html the parameters need to be of type tuple and there should be one %s for each of them.
I would suggest the following changes:
gender_values = ('Male', 'Female')
registeredQuery = "SELECT COUNT(DISTINCT uid) AS registered FROM `users` WHERE gender IN (%s, %s)"
placeholders = gender_values
Dynamic gender values
registeredQuery = "SELECT COUNT(DISTINCT uid) AS registered FROM `users` WHERE gender IN ("
for i in range(len(gender_values)):
if i< len(gender_values)-1:
registeredQuery += " %s,"
else:
registeredQuery += " %s)"
placeholders = gender_values # or tuple(gender_values) if they are a list
Unfortunately I've no Idea what's the issue coding in this way. I try to run a select statement within a class - the result is: TypeError: open_select() missing 1 required positional argument: 'query'.
Does anybody have an idea? Thx in advance
class finDB_exec:
def __init__(self):
self.dbcn = db.connect(host='localhost',
user='root',
password='xxx',
database='xxxx')
self.dbcr = self.dbcn.cursor()
def db_commit(self):
self.dbcn.commit()
def db_close(self):
self.dbcr.close()
self.dbcn.close()
##############################################################################
#### 1) Open Select
##############################################################################
def open_select(self,query):
try:
self.cr = self.dbcr()
self.cr.execute(query)
self.db_commit()
result = self.cursor.fetchall()
return result
except:
pass
query = 'Select * from tbl_companies limit 10'
res = finDB_exec.open_select(query)
I am tried to retrieve entry_time from mysql table (named user) and then find the time difference between now (exitTime) and the entry_time. My code doesn't seem to recover the data from mysql table. f is the id search key for the code which searches against the ID (PRIMARY KEY) of the mysql database to find the corresponding entry_time.
I have tried taking it as a string as well to see if i could retrieve the value but to no avail.
from tkinter import *
import time,datetime
import mysql.connector as mc
from tkinter import messagebox
import cv2
import matplotlib.pyplot as plt
import sys
import time
def billCalc():
global exitTime
global EntryTime
EntryTime = datetime.datetime
exitTime = datetime.datetime.now()
try:
conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')
except mc.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
sql_Query = "SELECT `entry_time` FROM `user` WHERE `ID` ="+f.get()
#id = (f.get(),)
print(record[1])
cursor = conn.cursor(buffered=True)
cursor.execute(sql_Query, id)
record = cursor.fetchone()
# selecting column value into varible
EntryTime = datetime(record[1])
print(record[1])
conn.close()
print(exitTime)
print(EntryTime)
print(f.get())
BillTime = EntryTime - exitTime
Bill = BillTime * 2
print(Bill.get())
def main_screen():
screen = Tk()
screen.geometry("300x400")
screen.title("EXIT GATE")
global f
f = StringVar()
Entry(screen, textvariable=f).place(x=150, y=200)
print(f.get())
Button(screen,text="Exit",height='2',width='15',command=billCalc).place(x=150,y=300)
screen.mainloop()
main_screen()
It is supposed to take an exit time using datetime.datetime.now() which it does.
Then it is supposed to take input ID from the user to search in the database.
After that it is supposed to retrieve the corresponding entry time
Find the difference between entrytime and exit time in seconds
Then provide a bill.
EDITED AND FIXED CODE:
from tkinter import *
import datetime
import mysql.connector as mc
from tkinter import messagebox
import cv2
import matplotlib.pyplot as plt
import sys
import time
def billCalc():
global exitTime
global EntryTime
EntryTime = datetime.datetime
exitTime = datetime.datetime.now()
try:
conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')
except mc.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
cursor = conn.cursor()
cursor.execute("SELECT entry_time FROM user where id="+f.get())
record = cursor.fetchone()
EntryTime = (record[0])
conn.close()
print(exitTime)
print(EntryTime)
print(f.get())
BillTime = exitTime - EntryTime
print(BillTime)
def main_screen():
screen = Tk()
screen.geometry("300x400")
screen.title("EXIT GATE")
global f
f = StringVar()
Entry(screen, textvariable=f).place(x=150, y=200)
print(f.get())
Button(screen,text="Exit",height='2',width='15',command=billCalc).place(x=150,y=300)
screen.mainloop()
main_screen()