我在这个问题上遇到了很多麻烦,并且一直在检查堆栈溢出和web上的所有问题,以寻找答案。我试图在这个语句中替换一个变量(“DELETE FROM where USERNAME=”+[variable])
这是我的代码: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()
感谢大家的帮助。谢谢:)
编辑:这就是我遇到的错误mysql.connector.errors.ProgrammingError:1054(42S22):“where子句”中的“Hoyt”列未知