使用Python更新MySQL数据库

发布于 2025-02-02 20:24:35 字数 1889 浏览 3 评论 0原文

我遇到了一些问题来更新数据库,它不断抛出相同的错误

编程: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()

任何帮助将是最受赞赏的。
请注意,顶部用于在数据库中插入数据,并且没有任何问题起作用。

请参阅链接到错误的屏幕截图。

更新菜单 errors

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.

Update menu
errors

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚气少女 2025-02-09 20:24:35

语法错误是由于要设置的列列表后的额外逗号造成的。

但是您也缺少要将列设置为的值。您不能在查询中使用Python变量。您应该将占位符放在那里,并将其作为第二个参数将其传递给cursor.execute()

updated_employee = """UPDATE employee_db 
                      SET Name = %s,
                         Surname = %s,
                         Address = %s,
                         Credentials = %s,
                         Department = %s,
                         Hourly_rate = %s,
                         email = %s
                      WHERE ID = %s;"""
cursor.execute(updated_employee, (name, surname, address, credentials, department, hr_rate, email, id))
employee_db.commit()

设置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().

updated_employee = """UPDATE employee_db 
                      SET Name = %s,
                         Surname = %s,
                         Address = %s,
                         Credentials = %s,
                         Department = %s,
                         Hourly_rate = %s,
                         email = %s
                      WHERE ID = %s;"""
cursor.execute(updated_employee, (name, surname, address, credentials, department, hr_rate, email, id))
employee_db.commit()

There's no point in setting the id column, since you're not changing it -- you're using id in the WHERE clause to select the row with that ID.

眉黛浅 2025-02-09 20:24:35

这是代码的完整行。

def update():
display_db()
cursor = employee_db.cursor()
db_connect()
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
    }]
    
employee = emp
db_connect()

for item in employee:
    employee_df = pd.DataFrame([item])
    display(employee_df)
    db_conn = create_engine("mysql+mysqldb://root:root@localhost/employee_db")
    employee_df.to_sql(con=db_conn, name='employee_data', if_exists='replace', index=False)

# Update employee
updated_employee = """UPDATE employee_db 
                      SET Name = %s,
                          Surname = %s,
                          Address = %s,
                          Credentials = %s,
                          Department = %s,
                          Hourly_rate = %s,
                          email = %s
                      WHERE ID = %s;"""
cursor.execute(updated_employee, (name, surname, address, credentials, department, hr_rate, email, id))
cursor.execute(updated_employee)
employee_db.commit()
display_db()

“新”错误的屏幕截图...
更新菜单
errors

ProgrammingError: 1146 (42S02): Table 'employee_db.employee_db' doesn't exist

Here is the complete line of codes.

def update():
display_db()
cursor = employee_db.cursor()
db_connect()
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
    }]
    
employee = emp
db_connect()

for item in employee:
    employee_df = pd.DataFrame([item])
    display(employee_df)
    db_conn = create_engine("mysql+mysqldb://root:root@localhost/employee_db")
    employee_df.to_sql(con=db_conn, name='employee_data', if_exists='replace', index=False)

# Update employee
updated_employee = """UPDATE employee_db 
                      SET Name = %s,
                          Surname = %s,
                          Address = %s,
                          Credentials = %s,
                          Department = %s,
                          Hourly_rate = %s,
                          email = %s
                      WHERE ID = %s;"""
cursor.execute(updated_employee, (name, surname, address, credentials, department, hr_rate, email, id))
cursor.execute(updated_employee)
employee_db.commit()
display_db()

screenshot of "new" errors...
Update menu
Errors

ProgrammingError: 1146 (42S02): Table 'employee_db.employee_db' doesn't exist
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文