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
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).
My Python Script
from telethon import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import ImportChatInviteRequest
import json
import time
import asyncio
file_json = open("data.json")
data = json.loads(file_json.read())
api_id = MY_ID
api_hash = 'MY_HASH'
u1 = TelegramClient('u1', api_id, api_hash)
async def main():
me = await u1.get_me()
c1 = await u1.get_entity("#somechannel")
await u1(JoinChannelRequest(c1))
with u1:
u1.loop.run_until_complete(main())
My Json.File
{
"c1" : "#somechannel"
}
how to read data from json file to join channel c1
TIA sorry for bad english
nvm i solve my problem
#IMPORTING MODULE
from telethon import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import ImportChatInviteRequest
from telethon.tl.functions.channels import LeaveChannelRequest
import time
import asyncio
import json
#API ID & API HASH
api_id = XXXXX
api_hash = 'XXXXXXXXXXXXXXXXXX'
#JSON LOADS
file_json = open("data.json")
data = json.loads(file_json.read())
#JSON LOADS VALUE
bot = (data["bot"])
ch1 = (data["ch1"])
ch2 = (data["ch2"])
ch3 = (data["ch3"])
ch4 = (data["ch4"])
ch5 = (data["ch4"])
ch6 = (data["ch4"])
ch7 = (data["ch4"])
ch8 = (data["ch4"])
ch9 = (data["ch4"])
ch10 = (data["ch4"])
#START USER 1
u01 = TelegramClient('u01', api_id, api_hash)
async def main():
me = await u01.get_me()
username = me.username
print("User Sekarang :", username)
with u01:
u01.loop.run_until_complete(main())
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
I'm writing a code to generate n-grams for every record in the table by reading a specific column.
def extract_from_db(inp_cust_id):
sql_db = TatDBHelper()
t_sql = "select notes from raw_data where customer_id = {0}"
db_data = sql_db.execute_read(t_sql.format(inp_cust_id))
for row in db_data:
text = row.values()
bi_grams = generate_ngrams(text[0].encode("utf-8"), 2)
print bi_grams
def generate_ngrams(sentence, n):
sentence = sentence.lower()
sentence = re.sub(r'[^a-zA-Z0-9\s]', ' ', sentence)
tokens = [token for token in sentence.split(" ") if token != ""]
ngrams = zip(*[tokens[i:] for i in range(n)])
return [" ".join(ngram) for ngram in ngrams]
I'm getting the output like:
['i highly', 'highly recommend', 'recommend it']
['the penguin', 'penguin encounter', 'encounter was', 'was awesome']
I want the output to look like below, can anybody help me to get this.
['i highly',
'highly recommend',
'recommend it',
...
]
creat another list all_ngrams, and keep appending the values to it , using .extend(), and finally you will have all the ngrams in one list.
Try this :
def extract_from_db(inp_cust_id):
sql_db = TatDBHelper()
t_sql = "select notes from raw_data where customer_id = {0}"
db_data = sql_db.execute_read(t_sql.format(inp_cust_id))
all_ngrams = []
for row in db_data:
text = row.values()
bi_grams = generate_ngrams(text[0].encode("utf-8"), 2)
all_ngrams.extend(bi_grams)
print all_ngrams
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()