使用Python更新MySQL数据库
我遇到了一些问题来更新数据库,它不断抛出相同的错误
编程:1064(42000):您的SQL语法中有错误;查看与您的MySQL Server版本相对应的手册,以获取正确的语法,该语法在第10行中附近使用“ Where id = ID”的手册
。数据库更新功能。
def update():
display_db()
cursor = employee_db.cursor()
id = int(input('Selected Employee ID to be updated: '))
sql = "SELECT * FROM employee_db.employee_data;"
db_conn = create_engine("mysql+mysqldb://root:root@localhost/employee_db")
data = pd.read_sql_query(sql, db_conn)
df = pd.DataFrame(data)
name = input('Enter the Empoyee name: ')
surname = input('Enter Empoyee surname: ')
address = input('Enter Empoyee address: ')
credentials = input('Enter Empoyee credentials: ')
department = input('Enter Empoyee department: ')
hr_rate = input('Enter Empoyee hourly rate: ')
email = name[0] + surname + '@avengers.av'
email = email.lower()
emp = [{
'ID': id,
'Name': name,
'Surname': surname,
'Address': address,
'Credentials': credentials,
'Department': department,
'Hourly_rate': hr_rate,
'email': email
}]
# Update employee
updated_employee = """UPDATE employee_db
SET ID = id,
Name = name,
Surname = surname,
Address = address,
Credentials = credentials,
Department = department,
Hourly_rate = hr_rate,
email = email,
WHERE ID = id;
cursor.execute(updated_employee)
employee_db.commit()
display_db()
任何帮助将是最受赞赏的。
请注意,顶部用于在数据库中插入数据,并且没有任何问题起作用。
请参阅链接到错误的屏幕截图。
I having some issues to update my database, it keeps throwing the same error
ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = id' at line 10
The database update function.
def update():
display_db()
cursor = employee_db.cursor()
id = int(input('Selected Employee ID to be updated: '))
sql = "SELECT * FROM employee_db.employee_data;"
db_conn = create_engine("mysql+mysqldb://root:root@localhost/employee_db")
data = pd.read_sql_query(sql, db_conn)
df = pd.DataFrame(data)
name = input('Enter the Empoyee name: ')
surname = input('Enter Empoyee surname: ')
address = input('Enter Empoyee address: ')
credentials = input('Enter Empoyee credentials: ')
department = input('Enter Empoyee department: ')
hr_rate = input('Enter Empoyee hourly rate: ')
email = name[0] + surname + '@avengers.av'
email = email.lower()
emp = [{
'ID': id,
'Name': name,
'Surname': surname,
'Address': address,
'Credentials': credentials,
'Department': department,
'Hourly_rate': hr_rate,
'email': email
}]
# Update employee
updated_employee = """UPDATE employee_db
SET ID = id,
Name = name,
Surname = surname,
Address = address,
Credentials = credentials,
Department = department,
Hourly_rate = hr_rate,
email = email,
WHERE ID = id;
cursor.execute(updated_employee)
employee_db.commit()
display_db()
Any help will be the most appreciated.
Note that the top part was used to insert data in the database and it worked without any issue.
See link to screenshot of error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
语法错误是由于要设置的列列表后的额外逗号造成的。
但是您也缺少要将列设置为的值。您不能在查询中使用Python变量。您应该将占位符放在那里,并将其作为第二个参数将其传递给
cursor.execute()
。设置
ID
列没有意义,因为您没有更改它 - 您正在使用id
在中使用
strape oflect带有该ID的行。The syntax error is due to the extra comma after the list of columns to set.
But you're also missing the values that you want to set the columns to. You can't use python variables in the query. You should put placeholders there and pass the values as the second argument to
cursor.execute()
.There's no point in setting the
id
column, since you're not changing it -- you're usingid
in theWHERE
clause to select the row with that ID.这是代码的完整行。
“新”错误的屏幕截图...
更新菜单
errors
Here is the complete line of codes.
screenshot of "new" errors...
Update menu
Errors