There was a change from 1.4 to 2.0. The above code will run fine with sqlalchemy version 1.4 I believe. setting SQLALCHEMY_WARN_20=1 python and running the above code reveals this warning:
<stdin>:1: RemovedIn20Warning: The Engine.execute() method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by the Connection.execute() method of Connection, or in the ORM by the Session.execute() method of Session. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
So the correct way to do the code now is:
with engine.connect() as conn:
result = conn.execute(stmt)
source here describing the behavior in 1.4 and here describing the behavior in 2.0
Answer from jonathan-dufault-kr on Stack OverflowThere was a change from 1.4 to 2.0. The above code will run fine with sqlalchemy version 1.4 I believe. setting SQLALCHEMY_WARN_20=1 python and running the above code reveals this warning:
<stdin>:1: RemovedIn20Warning: The Engine.execute() method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by the Connection.execute() method of Connection, or in the ORM by the Session.execute() method of Session. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
So the correct way to do the code now is:
with engine.connect() as conn:
result = conn.execute(stmt)
source here describing the behavior in 1.4 and here describing the behavior in 2.0
sql = f"""INSERT INTO user_apis
(api_id, user_id, monthly_requests, total_requests)
VALUES (1, {current_user.id}, 0, 0)"""
1.x
result = db.engine.execute(sql)
2.x
from sqlalchemy import text
with db.engine.begin() as conn:
result = conn.execute(text(sql))
conn.commit() #2.x execute now only works with SELECT.
#Inserts, Updates and Deletes now must be
#in a transaction and explicitly committed
#use engine echo=True to show transaction status
SELECT sql
with db.engine.connect() as conn:
result = conn.execute(text(sql)).fetchall()
Add this method to do the right thing on both versions.
Then change all your db.engine.execute(sql) to myengine_execute(sql)
def myengine_execute(sql):
#If sqlalchemy version starts with 1.4 then do it the old way
sqlalchemy_version = version("sqlalchemy")
if sqlalchemy_version.startswith('1.4.'):
with engine.connect() as conn:
return conn.execute(text(sql))
else:
#otherwise do it the new way with transactions:
with engine.connect() as conn:
result = conn.execute(text(sql))
#print(result.inserted_primary_key())
conn.commit()
upd_sql = "update bankaccount set amount = amount+5e10 where id = 1234567"
result = myengine_execute(upd_sql)
Unable to Validate Expectation Suit with PostgresSQL (No execute Attribute for Engine Object)
AttributeError: 'Engine' object has no attribute 'character_set_name'
Sqlite error
I keep getting this message ('NoneType' object has no attribute 'cursor') on one of my old projects any thoughts?
You have got an error because the getDbConnection function failed to connect to the database and hence returned None. Apart from this the whole structure of this program looks wrong. Why are you calling `getDbConnection` inside closeDbConnection ? You don't need to execute USE Enrolment everytime. You can refer to a table using Enrolment.<Table_name>. Refer to https://flask.palletsprojects.com/en/1.1.x/tutorial/database/ and https://flask.palletsprojects.com/en/1.1.x/patterns/sqlite3/ to know more about database access inside a flask app.