pymysql.err.OperationalError: (1364, "Field 'CameraID' doesn't have a default value")

pymysql.err.OperationalError: (1364, "Field 'CameraID' doesn't have a default value")

我在使用 MYSQL 代码时遇到问题。当我尝试将新相机输入数据库时​​,我无法将数据库设置为 运行。

这是我的做法:

然后在点击提交后,我看到了 1364 错误:字段“------”没有默认值。我曾尝试在我的 Navicat 中禁用 "strict_trans_table" 设置,但这对我来说仍然不是一个有效的解决方案。另外,不要介意 mysql 登录详细信息已被删除;我是故意的。

我的 Navicat SQL 上的 'CameraID' 值应该是每次创建新相机时都会计算的 ID。这是它的样子: 如您所见,它应该是一个唯一的 ID,但我还没有让它工作:( 我很感激我能得到的所有帮助。

from flask import Flask, render_template, request, redirect, session,url_for
import pymysql, datetime, os

app = Flask(__name__)
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app

#database connection function
def create_connection():
    return pymysql.connect(
        host='',
        user='',
        password='',
        db='',
        charset='utf8mb4'
        ,cursorclass=pymysql.cursors.DictCursor
    )

@app.route('/')
def home():
    """Renders a sample page."""
    return  render_template("index.html",title="Home")

#  read  or list records in cameras table
@app.route("/cameras", methods = ["GET", "POST"])
def cameras():
    connection=create_connection()
    with connection.cursor() as cursor:
        sql="SELECT * From tblcameras ORDER By CameraID DESC"
        cursor.execute(sql)
        #fetch all cameras into a list
        cameras = cursor.fetchall()
    return render_template("cameras.html", cameras = cameras, title="cameras Listing")
    #return redirect(url_for('cameras'))
# create
@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"]

        #photo=
        with connection.cursor() as cursor:
        # Create a new record
          sql = "INSERT INTO `tblcameras` (Company, Model) VALUES (%s,%s)"
          val=(Company, Model)
          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")

#camera
# edit
@app.route("/edit_camera", methods = ["GET", "POST"])
def editcamera():
    camera_id = request.args.get('id')# get the id parameter value
    connection=create_connection()
    if request.method =="POST":
        get = request.form
        Company = get["Company"]
        Model = get["Model"]
        #picture=
        with connection.cursor() as cursor:
        # Update record
                update_sql = "UPDATE cameras SET cameras.Company = %s,cameras.Model=%s WHERE cameras.CameraID = %s"
                values=(Company,Model,camera_id)
                cursor.execute(update_sql,(values))
                #save or commit values in dbase
                connection.commit()
                cursor.close()
                return redirect("/cameras")
    return render_template("camera.html", title ="Editing New camera")

#details
@app.route("/camera")
def camera():
    CameraID = request.args.get('id')# get the id parameter value
    connection=create_connection()
    with connection.cursor() as cursor:
        sql="SELECT * From tblcameras WHERE CameraID=%s"
        values=(CameraID)
        cursor.execute(sql, (values))
        #fetch camera with specified Id
        camera = cursor.fetchone()
    return  render_template("camera.html", camera=camera)

# delete
@app.route("/delete_camera", methods = ["GET", "POST"])
def deletecamera():
    camera_id = request.args.get('id')# get the id parameter value
    connection=create_connection()
    if request.method =="POST":
        get = request.form
        CameraID = get["Id"]
        with connection.cursor() as cursor:
        # delete record
           delete_sql = "DELETE FROM tblcameras WHERE CameraID=%s"
           value=(int(camera_id),)
           cursor.execute(delete_sql,value)
            #save values in dbase
           connection.commit()
           print(cursor.rowcount, "record(s) deleted")
           cursor.close()
           return redirect("/cameras")
    return render_template("camera.html", title ="Deleting camera")

if __name__ == '__main__':
    import os
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    app.run(HOST, PORT,debug=True)

通过单击主键旁边的键图标,将出现一个名为 auto increment 的复选框。如果选中此复选框,则无需指定 ID,它们将自动生成。