MYSQL Database error: "pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your-"

MYSQL Database error: "pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your-"

原标题:pymysql.err.ProgrammingError:(1064,"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 'Condition, KitLens, PurchasePrice) VALUES ('Nikon', 'HEWWO', '2020-06-11', 'OHH ' at line 1")

我在 运行 MYSQL 相机数据库中不断收到此错误。尝试创建新相机时,出现错误:

pymysql.err.ProgrammingError: (1064, "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 'Condition, KitLens, PurchasePrice) VALUES ('Nikon', 'HEWWO', '2020-06-11', 'OHH ' at line 1")

我尝试在 Navicat、我的 app.py 文件和 html 文件中排除故障并修复 MYSQL table,但无济于事。我在想这可能是因为代码中有一些特定的空格? 这是我用于创建新相机的代码片段,我确信它只需要在这里分享,因为它是问题的一部分:

    @app.route("/new_camera", methods = ["GET", "POST"])
    def newcamera():
        connection=create_connection()
        if request.method =="POST":
            get = request.form
            Company = get["Company"]
            Model = get["Model"]
            PurchaseDate = get["PurchaseDate"]
            Condition = get["Condition"]
            KitLens = get["KitLens"]
            PurchasePrice = get["PurchasePrice"]

            #photo=
            with connection.cursor() as cursor:
            # Create a new record
              sql = "INSERT INTO `tblcameras` (Company, Model, PurchaseDate, Condition, KitLens, PurchasePrice) VALUES (%s, %s, %s, %s, %s, %s)"
              val=(Company, Model, PurchaseDate, Condition, KitLens, PurchasePrice)
              cursor.execute(sql, val)
              #save values in dbase
              connection.commit()
              cursor.close()
              return redirect("/cameras")
        return redirect(url_for('cameras?do=new', title="Add New camera"))
        #return render_template("cameras.html",title="Adding New camera")

和我添加新相机html代码:

   {% extends "layout.html" %}
    {% block content %}

    {% if (request.args.get('do')=='new' )%}
<br />
<h2>New camera</h2>
<form method="post" action="/new_camera" class="was-validated">

    <div class="form-group">
        <label for="Company">Company:</label>
         <select id="Company" name="Company" class="form-control">
            <option value="">Choose an option</option>
            <option value="Canon">Canon</option>
            <option value="Nikon">Nikon</option>
         </select>
        <div class="valid-feedback">Valid.</div>
        <div class="invalid-feedback">Please fill out this field.</div>
    </div>
    <div class="form-group">
        <label for="Model">Model:</label>
        <input type="text" class="form-control" id="Model" placeholder="Enter Model" name="Model" required>
        <div class="valid-feedback">Valid.</div>
        <div class="invalid-feedback">Please fill out this field.</div>
    </div>

    <div class="form-group">
        <label for="PurchaseDate">Purchase Date:</label>
        <input type="date" class="form-control" id="PurchaseDate" placeholder="Purchase Date" name="PurchaseDate" required>
        <div class="valid-feedback">Valid.</div>
        <div class="invalid-feedback">Please fill out this field.</div>
    </div>

    <div class="form-group">
        <label for="Condition">Condition:</label>
        <input type="text" class="form-control" id="Condition" placeholder="Enter Condition" name="Condition" required>
        <div class="valid-feedback">Valid.</div>
        <div class="invalid-feedback">Please fill out this field.</div>
    </div>

    <div class="form-group">
        <label for="Kit Lens">Kit Lens</label>
        <input type="text" class="form-control" id="KitLens" placeholder="Enter Kit Lens (mm) or Standard" name="KitLens" required>
        <div class="valid-feedback">Valid.</div>
        <div class="invalid-feedback">Please fill out this field.</div>
    </div>

    <div class="form-group">
        <label for="Purchase Price">Purchase Price</label>
        <input type="number" min="1" step="any" class="form-control" id="PurchasePrice" placeholder="Enter Purchase Price ($)" name="PurchasePrice" required>
        <div class="valid-feedback">Valid.</div>
        <div class="invalid-feedback">Please fill out this field.</div>
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>
<hr />
{% endif %}

<br />

<h2>
    Existing cameras
</h2>

<div class="table-responsive">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th>Action</th>
            </tr>
        </thead>

        {%for camera in cameras%}
        <tr>
            <td>{{camera.Company}} {{camera.Model}}</td>
            <td> 
            <a href="/camera?do=edit&id={{camera.cameraId}}" class="btn btn-info">Edit</a> 
            <a href="/camera?do=details&id={{camera.cameraId}}" class="btn btn-success">Details</a> 
            <a href="/camera?do=delete&id={{camera.cameraId}}" class="btn btn-danger">Delete</a></td>
        </tr>

        {%endfor%}

    </table>
</div>

{% endblock %}

这里也是我尝试填写的数据截图:

Adding A New Camera

我尝试查看与此类似的其他建议问题和错误,但我无法在其中找到适合我的上下文的解决方案。如果您需要更多信息,请告诉我,我们将不胜感激。谢谢

条件是reserved word

sql = "INSERT INTO `tblcameras` (Company, Model, PurchaseDate,`Condition`, KitLens, PurchasePrice) VALUES (%s, %s, %s, %s, %s, %s)"

像 table

一样使用反引号

我遇到了同样的问题,因为我使用了保留字,比如name和rank。