So, I am playing with Python and mysql.
I built a database with 2 tables in it. I have a csv file with information to put in the tickers table. Now I have some information and need to continually update this table. However, I don’t want to put in data if that data is already there (the data I put in does not change over time).
However, when I try to find known data in the tickers table, the rows back from the fetchone() is “None”. I know I am asking for the correct information since I tried the Select statement from MySQL workbench and get stuff back.
Any help is greatly appreciated since I am just starting to use Python and MySQL.
Here is my code:
import pandas as pd
import mysql.connector
import DBcm
mydb = mysql.connector.connect(
host= '',
user= "",
password= "",
database= ""
)
mycursor = mydb.cursor()
inputFileName = "tickersfile.csv"
df = pd.read_csv(inputFileName)
df.head()
ticker_list = df.values.tolist() #change dataframe to list
for ticker in ticker_list:
#first, search for the ticker to verify that this is a new item.
_SQL = """select ticker from tickers where ticker = '%s'"""
sql_search = "select ticker from ticker where ticker = '" + ticker[0] + "'"
mycursor.execute(_SQL, sql_search)
row = mycursor.fetchone()
mydb.commit()
if row > 0:
print("Found ticker " + ticker)
else:
print("Ticker " + ticker + "not in database")