I have been having a lot of trouble with this issue and have been checking all over stack overflow and the web for an answer. I am trying to substitute a variable in this statement ("DELETE FROM where USERNAME =" + [variable])
Here is my code:
import mysql.connector as mysql
global usr_to_rmv
usr_to_rmv = 'Hoyt'
global from_table
from_table = "accounts"
global from_db
from_db = "users"
global from_column
from_column = "username"
cnx = mysql.connect(user = 'root', password = 'mag1c1234',
host = 'localhost',
database = from_db)
cursor = cnx.cursor()
# Uncomment to reset new data to 1
cursor.execute("ALTER TABLE accounts AUTO_INCREMENT = 1")
removeuser = ("DELETE FROM" + " " + from_table + " " + "WHERE" + " " + from_column + "=" + usr_to_rmv);
cursor.execute(removeuser)
query_1 = ("SELECT * FROM accounts");
cursor.execute(query_1)
for row in cursor.fetchall():
print(row)
cnx.commit()
cursor.close()
cnx.close()
All help appreciated. Thanks :)
Edit: This the error i am encountering mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'Hoyt' in 'where clause'