I have a list called headers of about 40-50 items, and I'm trying to make them all keys in a table. I'm trying to avoid typing them all out, here's what I have so far:
def createtable_db():
cnx = mysql.connector.connect(user='x', password='x', host='x', database='data')
mycursor = cnx.cursor()
create_table = "CREATE TABLE IF NOT EXISTS players (id INT AUTO_INCREMENT PRIMARY KEY)"
for h in headers:
pop_table = "ALTER TABLE players ADD COLUMN "+ h +" VARCHAR(255)"
mycursor.execute(create_table)
mycursor.execute(pop_table)
cnx.close()
createtable_db()
This only adds the very last key, I tried adding a + to make this line:
pop_table += "ALTER TABLE players ADD COLUMN "+ h +" VARCHAR(255)"
but that's also not working: it gives me a syntax error.