Buddy Peacock wrote: > 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?
The variable name for name is :author. Therefore I think you have to use "author" as the key in the dict: db.execute("SELECT * FROM authors WHERE name = :author", {"author": author}) -- https://mail.python.org/mailman/listinfo/python-list