I am having a problem with MYSQL code. I cannot get my database to run when I try to enter a new camera into the database.
Here is what I do:
Then upon clicking submit, I am presented with the 1364 Error: Field "------" doesn't have a default value. I have tried disabling the "strict_trans_table" setting in my Navicat but that still wasn't a valid solution for me. Also, don't mind that the mysql login details are removed; I did that on purpose.
The 'CameraID' value on my Navicat SQL is supposed to be an ID that counts every time a new camera is created. Here's what it looks like:
As you can see it's supposed to be a unique ID however I haven't gotten it to work :( I appreciate all the help I can get.
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)
By clicking on the key icon beside the primary key, a checkbox called auto increment
will appear. If this checkbox is checked, you will not need to specify IDs, and they will be generated automatically.