Python string comparison, user input -> mariadb - python

I need help in string comparison. I know there are many questions with this topic but none of them seem to help in my problem. This program gets location coordinates according to user input and then checks if that location is already in database, if not, then location and its coordinates are updated there.
When user_input table in the database is empty, data is inserted to database without a problem. Problem lies in string comparison where existing locations from the database are compared with user input (item == loc), even when the record is already in the database, script tries to insert it there. How to make the comparison work? All the help is appreciated.
def cordinates(address):
import requests
url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" %
(address)
reply = requests.get(url).json()
loc = reply['results'][0]['address_components'][0]['long_name']
lat = reply['results'][0]['geometry']['location']['lat']
lng = reply['results'][0]['geometry']['location']['lng']
return (loc,lat,lng)
def selecting():
import pymysql
db = pymysql.connect('localhost','vagrant','vagrant'
,database='coordinatesdb')
cursor = db.cursor()
cursor.execute('select loc from user_input;')
results = cursor.fetchall()
cursor.close()
return results
def inserting(loc, lat, lng):
import pymysql
db = pymysql.connect('localhost','vagrant','vagrant',
database='coordinatesdb')
cursor = db.cursor()
new_row = "insert into user_input (loc, lat, lng) values (%s, %s, %s)"
cursor.execute(new_row, (loc, lat, lng))
db.commit()
cursor.close()
address = input('Type loc: ')
a=cordinates(address)
loc = a[0]
lat = a[1]
lng = a[2]
select_results = selecting()
if not select_results:
inserting(loc, lat, lng)
print('Inserting as table is empty')
exit
def cordinates(address):
import requests
url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" % (address)
reply = requests.get(url).json()
loc = reply['results'][0]['address_components'][0]['long_name']
lat = reply['results'][0]['geometry']['location']['lat']
lng = reply['results'][0]['geometry']['location']['lng']
return (loc,lat,lng)
def selecting():
import pymysql
db = pymysql.connect('localhost','vagrant','vagrant' ,database='coordinatesdb')
cursor = db.cursor()
cursor.execute('select loc from user_input;')
results = cursor.fetchall()
cursor.close()
return results
def inserting(loc, lat, lng):
import pymysql
db = pymysql.connect('localhost','vagrant','vagrant',
database='coordinatesdb')
cursor = db.cursor()
new_row = "insert into user_input (loc, lat, lng) values (%s, %s, %s)"
cursor.execute(new_row, (loc, lat, lng))
db.commit()
cursor.close()
address = input('Type loc: ')
a=cordinates(address)
loc = a[0]
lat = a[1]
lng = a[2]
select_results = selecting()
if not select_results:
inserting(loc, lat, lng)
print('Inserting as table is empty')
exit
for item in select_results:
if item == loc:
print('Record is already in the database')
quit()
inserting(loc, lat, lng)
print('Inserting')

Connect only once per program, not once per query.

Related

SQL in python script not returning all values

I am trying to run an execute statement with python in sql to return all the ids that match up with one value, in this case x. There are multiple items that should match the variable(x) that I am trying to pass through but when I run this line I only get one item in the list and the rest get left behind. When I execute the line in the sqlite browser it give me a table with all the matching data I want but for some reason this doesn't pass it all into the list.
cur.execute('SELECT movie_id FROM MG WHERE genre_id = ?', (x, ))
for x in cur:
midg.append(y[0])
Here is my entire code so far:
gnames = list()
cur.execute('SELECT genre_name FROM Genre')
for row in cur:
gnames.append(row[0])
print(gnames)
gid = list()
midg = list()
grating = list()
grate = dict()
for namew in gnames:
gid.clear()
grating.clear()
midg.clear()
cur.execute('SELECT genre_id FROM Genre WHERE genre_name = ?', (namew, ))
x = cur.fetchone()[0]
cur.execute('SELECT movie_id FROM MG WHERE genre_id = ?', (x, ))
for x in cur:
midg.append(y[0])
for z in midg:
cur.execute('SELECT movie_rating FROM Movie WHERE movie_id = ?', (z, ))
for row in cur:
grating.append(row[0])
gaverage = sum(grating)/len(grating)
grate[namew] = gaverage
try it.
#Install package
#pip install pyodbc
import pyodbc
server = 'server'
database = 'database'
username = 'username'
password = 'password'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT * from INFORMATION_SCHEMA.TABLES")
row = cursor.fetchone()
while row:
#TABLE_CATALOG
print(row[0])
print()
#TABLE_SCHEMA
print(row[1])
print()
#TABLE_NAME
print(row[2])
print()
# TABLE_TYPE
print(row[3])
print()
print('all columns')
print(str(row[0]), "-",str(row[1]), "-",str(row[2]), "-",str(row[3]))
row = cursor.fetchone()

I keep getting the error 'unread result found', no matter what I change

The code is given below which attempts to insert a value into the database and create one and insert if not available according to the usernames of the users.
I keep getting an error 'Unread result found' on the line highlighted with a comment below.
Thank you in advance!
def update_userDatabase(username,text_input):
available = False
mydb = mysql.connector.connect(host="localhost",user="root",passwd="1234",database="ChatBot")
mycursor = mydb.cursor()
now = datetime.datetime.utcnow()
mycursor.execute("SHOW TABLES")
result = mycursor.fetchone()
sg.popup()
if text_input == "":
for x in result:
sg.popup(x)
if x == username:
available = True
if available == True:
sql = """INSERT INTO {tab} (Date) VALUES (%s)""".format(tab=username)
val = (now.strftime('%Y-%m-%d %H:%M:%S'))
mycursor.execute(sql, val)
else:
sql = """CREATE TABLE {tab} (Date varchar(50),Questions varchar(20))""".format(tab=username)
mycursor.execute(sql)
sql = """INSERT INTO {tab} (Date) VALUES (%s)""".format(tab=username)
val = (now.strftime('%Y-%m-%d %H:%M:%S'))
*mycursor.execute(sql, val)* #THIS IS THE LINE WITH THE ERROR
elif username=="":
sql = "INSERT INTO %s (Questions) VALUES (%s)"
val = (text_input)
mycursor.execute(sql, val)

python sqlite3 - cursor vs connections - executing UPDATE statements

I am having trouble with executing UPDATE statements.
In the code below when I write con.execute() it writes to the database, but if I write c.execute() it doesn't write to the database.
Long comment is where the code in question lies.
Most Relevant snippet
def write_data(con, c):
geolocator = Nominatim()
table = c.execute('SELECT * FROM playerdata WHERE birth_place_long is null AND birth_place is not null').fetchall()
for i in table:
try:
location = getgeo(i[4], geolocator)
lat, long = location.latitude, location.longitude
except:
print(i[0])
''' changing to c.execute doesnt work '''
con.execute('UPDATE playerdata SET birth_place_lat = ?, birth_place_long = ? WHERE id = ?', (1, 1, i[0]))
print("failed" + i[1] + " " + i[2])
con.commit()
else:
c.execute('UPDATE playerdata SET birth_place_lat = ?, birth_place_long = ? WHERE id = ?', (lat, long, i[0]))
print(i[1] + " " + i[2])
con.commit()
con.close()
Full code below:
import csv
from geopy.geocoders import Nominatim
import sqlite3
import socket
import geopy.exc
import pandas as pd
def connect_db():
con = sqlite3.connect('c:/s_db2.sqlite3')
c = con.cursor()
return con, c
def getgeo(place, geolocator):
try:
location = geolocator.geocode(place)
return location
except (socket.timeout):
return getgeo(place, geolocator)
except geopy.exc.GeocoderTimedOut:
return 0
def write_data(con, c):
geolocator = Nominatim()
table = c.execute('SELECT * FROM playerdata WHERE birth_place_long is null AND birth_place is not null').fetchall()
for i in table:
try:
location = getgeo(i[4], geolocator)
lat, long = location.latitude, location.longitude
except:
print(i[0])
''' changing to c.execute doesnt work '''
con.execute('UPDATE playerdata SET birth_place_lat = ?, birth_place_long = ? WHERE id = ?', (1, 1, i[0]))
print("failed" + i[1] + " " + i[2])
con.commit()
else:
c.execute('UPDATE playerdata SET birth_place_lat = ?, birth_place_long = ? WHERE id = ?', (lat, long, i[0]))
print(i[1] + " " + i[2])
con.commit()
con.close()
while True:
write_data(connect_db()[0], connect_db()[1])
print("************* re-running ****************")
This line in your loop:
write_data(connect_db()[0], connect_db()[1])
connect_db() is being called twice in the same call of write_data().
This is similar to:
con_1, c_1 = connect_db()
con_2, c_2 = connect_db()
write_data(con_1, c_2)
You are mixing instances of the connections with cursors of other instances.
instead you may want:
con_1, c_1 = connect_db()
write_data(con_1, c_1)
The conn is using a connection instance which has its own setup, i.e autocommit. But, a curso, you need to define the autocommit set, or rather to persist the commit after executing the sql.
i.e use con.commit() after with con: c.execute()
NB, if this logic is executing after the try-catch block (i.e exception) you probably need to rollback before executing this one.

Python code not creating tables on the database but able to query the results postgres

My usecase is to write create a temp table in the postgres database and fetch records from it and insert into a different table.
The code i used is:
import psycopg2
import sys
import pprint
from __future__ import print_function
from os.path import join,dirname,abspath
import xlrd
import os.path
newlist = []
itemidlist = []
def main():
conn_string = "host='prod-dump.cvv9i14mrv4k.us-east-1.rds.amazonaws.com' dbname='ebdb' user='ebroot' password='*********'"
# print the connection string we will use to connect
# print "Connecting to database" % (conn_string)
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
dealer_id = input("Please enter dealer_id: ")
group_id = input("Please enter group_id: ")
scriptpath = os.path.dirname('__file__')
filename = os.path.join(scriptpath, 'Winco - Gusti.xlsx')
xl_workbook = xlrd.open_workbook(filename, "rb")
xl_sheet = xl_workbook.sheet_by_index(0)
print('Sheet Name: %s' % xl_sheet.name)
row=xl_sheet.row(0)
from xlrd.sheet import ctype_text
print('(Column #) type:value')
for idx, cell_obj in enumerate(row):
cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
#print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))
num_cols = xl_sheet.ncols
for row_idx in range(0, xl_sheet.nrows): # Iterate through rows
num_cols = xl_sheet.ncols
id_obj = xl_sheet.cell(row_idx, 1) # Get cell object by row, col
itemid = id_obj.value
#if itemid not in itemidlist:
itemidlist.append(itemid)
# execute our Query
'''
cursor.execute("""
if not exists(SELECT 1 FROM model_enable AS c WHERE c.name = %s);
BEGIN;
INSERT INTO model_enable (name) VALUES (%s)
END;
""" %(itemid,itemid))
'''
cursor.execute("drop table temp_mbp1")
try:
cursor.execute("SELECT p.model_no, pc.id as PCid, g.id AS GROUPid into public.temp_mbp1 FROM products p, \
model_enable me, products_clients pc, groups g WHERE p.model_no = me.name \
and p.id = pc.product_id and pc.client_id = %s and pc.client_id = g.client_id and g.id = %s"\
% (dealer_id,group_id)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
cursor.execute("select count(*) from public.temp_mbp1")
# retrieve the records from the database
records = cursor.fetchall()
# print out the records using pretty print
# note that the NAMES of the columns are not shown, instead just indexes.
# for most people this isn't very useful so we'll show you how to return
# columns as a dictionary (hash) in the next example.
pprint.pprint(records)
if __name__ == "__main__":
main()
The try except block in between the program is not throwing any error but the table is not getting created in the postgres database as i see in the data admin.
The output shown is:
Please enter dealer_id: 90
Please enter group_id: 13
Sheet Name: Winco Full 8_15_17
(Column #) type:value
[(3263,)]
Thanks,
Santosh
You didn't commit the changes, so they aren't saved in the database. Add to the bottom, just below the pprint statement:
conn.commit()

Python 3 + SQLite check

Hello
I have a question about SQLite functions, maybe.
So, question:
How to check if name I set in Python is in certain column?
Example:
name = 'John'
Table name = my_table
Column name = users
Code details:
C = conn.cursor()
Please
Use parameter in the query as required. See the attached example for better understanding.
Sample SQLite code for searching value in tables
import sqlite3 as sqlite
import sys
conn = sqlite.connect("test.db")
def insert_single_row(name, age):
try:
age = str(age)
with conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS USER_TABLE(NAME TEXT, AGE INTEGER);")
cursor.execute("INSERT INTO USER_TABLE(NAME, AGE) VALUES ('"+name+"',"+age+")")
return cursor.lastrowid
except:
raise ValueError('Error occurred in insert_single_row(name, age)')
def get_parameterized_row(name):
try:
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM USER_TABLE WHERE NAME = :NAME",
{"NAME":name})
conn.commit()
return cursor.fetchall()
except:
raise ValueError('Error occurred in get_parameterized_row(name)')
if __name__ == '__main__':
try:
return_id = insert_single_row("Shovon", 24)
return_id = insert_single_row("Shovon", 23)
return_id = insert_single_row("Sho", 24)
all_row = get_parameterized_row("Shovon")
for row in all_row:
print(row)
except Exception as e:
print(str(e))
Output:
('Shovon', 24)
('Shovon', 23)
Here I have created a table called USER_TABLE with two attributes: NAME and AGE. Then I inserted several values in the table and searched for a specific NAME. Hope it gives a way to start using SQLite in the project.

Categories

Resources