-1

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:
Making a new camera

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: enter image description here 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)
nbk
  • 45,398
  • 8
  • 30
  • 47
markiplier
  • 23
  • 9
  • How have you created your cameras table? I'd assume that you need to set CameraID to an autoincrementing primary key – Minion3665 May 27 '20 at 21:14
  • the id field has to be an auto_increment, as you don't show us the create table of that. check it yourself – nbk May 27 '20 at 21:15
  • The CameraID value right now already has two manually inputted values into the table, and they both start with 1 and 2. You can see the table [here](https://i.imgur.com/dwoBwYG.png) – markiplier May 27 '20 at 21:20
  • In Navicat, if you click on the key icon is there a checkbox labelled 'auto increment' or similar? If so, is it checked? – Minion3665 May 27 '20 at 21:21
  • Thank you! That seemed to do it, thank you very much for your help :) I had to tick the auto_increment box. – markiplier May 27 '20 at 21:25
  • @markiplier You're very welcome. I've added it as an answer, feel free to accept if you feel it to be correct :) – Minion3665 May 27 '20 at 21:28

1 Answers1

0

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.

Minion3665
  • 879
  • 11
  • 25