MYSQL数据库错误:“pymysql.err.ProgrammingError:(1064,“您的SQL语法有错误;请检查与您的SQL语法对应的手册”

2024-09-25 18:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

原始标题:pymysql.err.ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,了解使用接近‘条件、KitLens、采购价格)值(‘尼康’、‘HEWWO’、‘2020-06-11’、‘OHH’(第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表进行故障排除和修复,但均无效。我想这可能是因为代码中的某些空白? 以下是我创建新相机的代码片段,我确信这是问题的一部分,因此只需要在此处共享:

    @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

我试着查看其他类似于此的问题和错误,但在我的上下文中找不到任何解决方案。如果您需要更多信息,请让我知道,任何帮助都将不胜感激。多谢各位


Tags: divformidforgetmodelconditionlabel
1条回答
网友
1楼 · 发布于 2024-09-25 18:25:29

条件是reserved word

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

用你为你的桌子做的背面记号

相关问题 更多 >