SQLAlchemy & Postgresql

Abdur-Rahmaan Janhangeer arj.python at gmail.com
Thu May 28 14:44:40 EDT 2020


Well,


See

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)")

but i don't see any code for creating the authors table

also did you mean "name = author" instead of "name = :author"

3rd thing is that since you are using SQLALchemy, you should not be using
pure SQL

What i mean by models is something like this:
https://github.com/Abdur-rahmaanJ/shopyo/blob/dev/shopyo/modules/product/models.py

where you define code and your table is created the link uses
flask-sqlalchemy but pure sqlalchemy should
be somewhat similar

4th thing: when using

for isbn, title, author, year in reader:

without flask, do you actually see a value for author?

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy <https://compileralchemy.github.io/> | blog
<https://abdur-rahmaanj.github.io/>
github <https://github.com/Abdur-RahmaanJ>
Mauritius


On Thu, May 28, 2020 at 10:31 PM Buddy Peacock <buddy.peacock at gmail.com>
wrote:

> I'm not sure what you mean by models?  Am I missing something in my
> environment perhaps?
>
> Al (Buddy) Peacock, PMP, MCCT,  ITILv3, SMC, CSM, SPOC
> (920) 740-3411
> linkedin.com/in/buddypeacock <https://www.linkedin.com/in/buddypeacock/>
>
>
>
>
> On Thu, May 28, 2020 at 2:26 PM Abdur-Rahmaan Janhangeer <
> arj.python at gmail.com> wrote:
>
>> Greetings,
>>
>>
>> where did you define your models?
>>
>> Kind Regards,
>>
>>
>> Abdur-Rahmaan Janhangeer
>>
>> https://www.github.com/Abdur-RahmaanJ
>>
>> Mauritius
>>
>> sent from gmail client on Android, that's why the signature is so ugly.
>>
>> On Thu, 28 May 2020, 22:22 Buddy Peacock, <buddy.peacock at gmail.com>
>> 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?
>>>
>>>
>>> Al (Buddy) Peacock, PMP, MCCT,  ITILv3, SMC, CSM, SPOC
>>> (920) 740-3411
>>> linkedin.com/in/buddypeacock <https://www.linkedin.com/in/buddypeacock/>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>>


More information about the Python-list mailing list