MathiasRa
MathiasRa

Reputation: 835

Python: Adding column name as a variable to mysql table

I am struggling a bit with figuring out how I can alter mysql column names in python.

Below is what I ideally want to do:

column_name = "Kills"    
my_cur.execute("alter table match_historical_player_stats add  column_name float ")

Can someone help me create the correct syntax to solve this problem?

Upvotes: 0

Views: 630

Answers (2)

Jim Wright
Jim Wright

Reputation: 6058

You should use a prepared statement, rather that python formatting. This will make sure that everything is escaped properly.

column_name = "Kills"
query = "ALTER TABLE match_historical_player_stats ADD %s FLOAT"
my_cur.execute(query, (column_name,))

Upvotes: 0

madik_atma
madik_atma

Reputation: 799

You can try this here:

column_name = "Kills"
query = "ALTER TABLE match_historical_player_stats ADD {} FLOAT".format(column_name)

my_cur.execute(query)

Upvotes: 1

Related Questions