So I am trying to build a database in MySQL using python. I already created the database and created the table I want to use. it's called 'alliances'.
Now this table only has two columns, an ID column and a alliance_name column. The ID's auto increment , and the names I already have them as a list and would like to insert them into the table one by one. but it gives me an error:
ProgrammingError: 1054 (42S22): Unknown column 'alliance' in 'field list'
this is my code :
import mysql.connector
db = mysql.connector.connect(host = "localhost", user = "root", password = "#########", database = 'WANHUI')
mycursor = db.cursor()
# mycursor.execute("CREATE DATABASE WANHUI") we already added to the db
# mycursor.execute("CREATE TABLE alliances (ID INT PRIMARY KEY AUTO_INCREMENT, alliance_name VARCHAR(100) NOT NULL) ")
mini_list = alliance_list[:30] # we only need the first 30
for alliance in mini_list:
mycursor.execute("INSERT INTO alliances (alliance_name) VALUES (alliance)")
db.commit()
Why does it say no 'alliance' column if i do specify in the query to insert into the alliance_name and that 'alliance' is the string that I wish to insert ??
EDIT: I already tried this version:
for alliance in mini_list:
mycursor.execute("INSERT INTO alliances (alliance_name) VALUES (%s)", (alliance))
db.commit()
But it gives me this error:
ProgrammingError: Could not process parameters: str(Iron Brotherhood Alliance), it must be of type list, tuple or dict
and even if I do :
mycursor.execute("INSERT INTO alliances (alliance_name) VALUES (%s)", (mini_list))
db.commit()
it would give me this :
ProgrammingError: Not all parameters were used in the SQL statement