Python pymysql.err.OperationalError:(1364,“Field';CameraID';没有默认值)
我对MYSQL代码有问题。当我尝试将新相机输入数据库时,无法使数据库运行 以下是我所做的:Python pymysql.err.OperationalError:(1364,“Field';CameraID';没有默认值),python,mysql,database,default-value,navicat,Python,Mysql,Database,Default Value,Navicat,我对MYSQL代码有问题。当我尝试将新相机输入数据库时,无法使数据库运行 以下是我所做的: 然后单击submit,我会看到1364错误:字段“-----”没有默认值。我曾尝试在Navicat中禁用“strict_trans_table”设置,但这对我来说仍然不是一个有效的解决方案。另外,不要介意删除mysql登录详细信息;我是故意那样做的 我的Navicat SQL上的“CameraID”值应该是一个ID,它在每次创建新相机时都会计数。下面是它的样子: 正如你所看到的,它应该是一个独特的ID,
然后单击submit,我会看到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)
单击主键旁边的键图标,将出现一个名为“自动递增”的复选框。如果选中此复选框,则无需指定id,它们将自动生成。您是如何创建cameras表的?我假设您需要将CameraID设置为自动递增主键id字段d必须是一个自动增量,因为您没有向我们显示该值的创建表。请自己检查。CameraID值现在已经有两个手动输入到表中的值,它们都以1和2开头。您可以在Navicat中看到该表,如果您单击键图标,是否有标记为“自动增量”或类似的复选框?如果有,是否有勾选?谢谢!似乎可以,非常感谢你的帮助:)我必须勾选自动递增框。