SQLAlchemy & Postgresql

Buddy Peacock buddy.peacock at gmail.com
Thu May 28 14:22:10 EDT 2020


Hello group,
I have a pretty good background in MySQL & MSSQL as well as VB & Php, but I
am new to Python and Postgresql.

I am taking a class and working on a project to insert authors and books
into a table.  My code for this is:
===============================================================
import csv
import os

from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)

# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
# Session(app)

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

print (os.getenv("DATABASE_URL"))

def main():
    f = open("books.csv")
    reader = csv.reader(f)
    for isbn, title, author, year in reader:
        if db.execute("SELECT * FROM authors WHERE name = :author",
{"name": author}).rowcount == 0:
            db.execute("INSERT INTO authors (name) VALUES (author)")

        print(f" {author} was read.")
    db.commit()

if __name__ == "__main__":
    main()
====================================================================
If I comment out the "if" statement then the authors all print on screen.
But when I run this with the if statement I am getting a message that says:
ssqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) A
value is required for bind parameter 'author'
[SQL: SELECT * FROM authors WHERE name = %(author)s]
[parameters: [{'name': 'author'}]]

What am I missing?


Al (Buddy) Peacock, PMP, MCCT,  ITILv3, SMC, CSM, SPOC
(920) 740-3411
linkedin.com/in/buddypeacock <https://www.linkedin.com/in/buddypeacock/>


More information about the Python-list mailing list