Trying To Create New Entry In SQLite DB Results In 'DBAPI Error'

19 Views Asked by At

I am trying to add a section to my database using this code:

THIS IS ALL OF MY IMPORTS

from flask import Flask, render_template, redirect, url_for, request
from flask_bootstrap import Bootstrap5
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy import Integer, String, Float, String
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
import requests

THIS IS TO CREATE THE TABLE

class Movies(db.Model):
    id: Mapped\[int\] = mapped_column(db.Integer, primary_key=True)
    title: Mapped\[str\] = mapped_column(db.String\[50\], unique=True, nullable=False)
    year: Mapped\[int\] = mapped_column(db.Integer, unique=True, nullable=False)
    description: Mapped\[str\] = mapped_column(db.String\[500\], unique=True, nullable=False)
    rating: Mapped\[float\] = mapped_column(db.Float, nullable=False)
    ranking: Mapped\[int\] = mapped_column(db.Integer, unique=True, nullable=False)
    review: Mapped\[str\] = mapped_column(db.String\[300\], nullable=False)
    img_url: Mapped\[str\] = mapped_column(db.String\[1000\], nullable=False)

with app.app_context():
db.create_all()

THIS IS ADDING THE NEW ENTRY TO THE TABLE

with app.app_context():
    # THIS IS USING THE "Movies" CLASS THAT I MADE AT THE TOP
    new_movie = Movies(
        title="Phone Booth",
        year=2002,
        description="Publicist Stuart Shepard finds himself trapped in a \
                     phone booth, pinned down by an extortionist's sniper rifle. \
                     Unable to leave or receive outside help, Stuart's negotiation\
                     with the caller leads to a jaw-dropping climax.",
        rating=7.3,
        ranking=10,
        review="My favourite character was the caller.",
        img_url="https://image.tmdb.org/t/p/w500/tjrX2oWRCM3Tvarz38zlZM7Uc10.jpg"
        )

    db.session.add(new_movie)
    db.session.commit()

when running the code it gives me this error:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: movies.ranking
[SQL: INSERT INTO movies (title, year, description, rating, ranking, review, img_url) VALUES (?, ?, ?, ?, ?, ?, ?)]
[parameters: ('Phone Booth', 2002, "Publicist Stuart Shepard finds himself trapped in a                 phone booth, pinned down by an extortionist's sniper rifle.                 Unable to leave or receive outside help, Stuart's negotiation                with the caller leads to a jaw-dropping climax.", 7.3, 10, 'My favourite character was the caller.',  'https://image.tmdb.org/t/p/w500/tjrX2oWRCM3Tvarz38zlZM7Uc10.jpg')]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

this is just the main part of the error that I have extracted out from the mammoth of gibberish that the interpreter spat out it's rear end.

When I went to the link it told me something to do with a 'DBAPI ERROR' and 'DBAPI driver'. driver makes me think that it is something to do with drivers on my system but when I searched for it on the AUR (I'm using arch) it only shows me a python package 'python-jaydebeapi'.

I would really appreciate it if anyone could help me fix this. Thank You.

I was expecting for the program to create a database with a new entry. Unfortunately it is giving me a 'DBAPI ERROR'

0

There are 0 best solutions below