mysql - 使用 python 代码中的变量将值添加到 sql 时显示错误

标签 mysql python-3.x

每当我尝试使用 Python 中的 SQL 查询将新数据输入数据库时​​,它都会显示类似“您的 SQL 语法有错误;请检查与您的 MariaDB 服务器版本相对应的手册”的错误在 '('employee') 附近使用正确的语法。

问题出在(elif value == 2) block

import pymysql
import time

def student_database():

    storage = input("Enter your database name :")
    table = input("Enter your table name")
    print("Press 1 for Displaying Data -->")
    print("Press 2 for entering new Data..")
    print("press 3 for adding new column to  table..")
    print("press 4 for modifying data in table")
    value = int(input())
    db = pymysql.connect(host='localhost', database=storage, user='', passwd='')
    cursor = db.cursor()
    print("Connected to " + storage)

    if value == 1:
        print("Printing Data :\n")
        cursor.execute("SELECT * FROM (%s)" %(table))
        for row in cursor:
            print(row)
    elif value == 2:
        print("Enter New Values to be entered")
        first = input("Enter First Name: ")
        last = input("Enter Last Name: ")
        age = input("Enter Age: ")
        sex = input("Enter Sex: ")
        salary = input("Enter salary amount: ")
        no = input("Enter number")
        # query for adding data into table
        cursor.execute("INSERT INTO (%s) VALUES (%s, %s, %s, %s, %s, %s)", (table, first, last, age, sex, salary, no))
        db.commit()
        print("Data entered Succesfully")

    elif value == 3:
        print("Enter column name to be added")
        column_name = input("enter new column name")
        #query for adding new column in existing table
        cursor.execute("Alter table employee add %s VARCHAR(10)" %column_name)
        db.commit()
        print("column Added successfully")
    elif value == 4:
        field_name = input("enter field name to be updated :")
        new_value = input("Please enter your new value")
        reference_name = input("enter reference column name :")
        reference_value = input("enter reference value :")
        cursor.execute("update employee set %s = %s WHERE %s = %s" % (field_name, new_value, reference_name, reference_value))
        db.commit()
        print("data modified succesfully    ")

while(True):

    student_database()
    time.sleep(3)

如果value == 2,它需要接受一些输入并将相同的数据输入到员工表中。

最佳答案

您不能在准备好的语句中使用表名的占位符。您只能将它们用于数据值。您应该将cursor.execute 语句更改为:

cursor.execute("INSERT INTO "+table+" VALUES (%s, %s, %s, %s, %s, %s)", (first, last, age, sex, salary, no))

而且我不知道你在哪里为表变量赋予了值。

关于mysql - 使用 python 代码中的变量将值添加到 sql 时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56930226/

相关文章:

Mysql 从不同的表中选择

php - 将记录从 MySql 数据库加载到选择标记中

MySQL 可以根据两个日期字段之间的差异创建行吗?

python - 一副纸牌的随机迭代

mysql - InnoDB 设置修改对现有/新数据库的影响

MySQL 工作台 : Do not open new tab on "Select Rows"

python-3.x - Pycharm 文档字符串 : code references and docstring inheritance

Django 过滤器测试

json - Python Json 引用和验证

python-3.x - python : finding elements of a webpage to scrape in python when page content is loaded using Java script