I have a .csv file containing data of all stocks. I wanted to create a table for each stock with the table name as the stock's symbol, in a MySQL database daily_stock_recorder.
There are about 1900 items and obviously it wouldnt be feasible to write them one by one, so I created this python program, which takes the stock_symbol of each row and makes a table for it. Here's the code:
import mysql.connector as mc
import pandas as pd
mycon=mc.connect(host='localhost',user='root',passwd='1234',database='daily_stock_recorder')
cursor=mycon.cursor()
stock_df = pd.read_csv(r'C:\Users\Tirth\Desktop\pyprojects\all_stocks.csv')
for i in range(0, len(stock_df)):
stock=stock_df['SYMBOL'][i]
for j in range(0,len(stock)):
if ord(stock[j])==38:
stock_ticker=stock.replace(stock[j],'')
else:
stock_ticker=stock.replace(stock[j],stock[j])
todo="create table %s (close_price decimal(10,8) not null, traded_value bigint not null,
traded_quantity bigint not null, date_recorded date not null unique)"%(stock_ticker)
cursor.execute(todo)
mycon.commit()
As you can see I have used a 'if' statement to replace the character '&' (ASCII Value = 38) in the symbol with '' (nothing). This is because every time I run the code, it is able to create tables successfully but then gives the error when it comes to the symbol 'COX&KINGS'. I am assuming that Mysql doesnt take table names with special characters.
But even after executing with the 'if' statement for replacing the special character, I get the same name without replacement of '&' and it lands on the same error.
Can You please point out what I am doing wrong?
By the way, this is the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '&KINGS (close_price decimal(10,8) not null)' at line 1
Thanks!