Flask Sqlalchemy No Errors but not updating sqlite3 virtual table - python

I have a small flask app that I am working on. It consists of a search box, which is using a select statement outside of Sqlalchemy to do a "full text search" on a virtual fts5 table
sqlstr = "SELECT * FROM items INNER JOIN Restaurants on Restaurants.rest_id = items.restaurant_id WHERE items MATCH '{}' ORDER BY item_rank DESC".format(q)
crsr = conn.execute(sqlstr)
search_data = crsr.fetchall()
crsr.close()
The user gets the results and then can THumbs up a menu item from the results,when that happens I take them to a "vote" flask route to record the thumbs up AND make a log in my analytics table. and then redirect to last page. This is where it gets wonky.
db.session.query(items).filter(items.id==item_id).update({items.item_rank:items.item_rank +1})
db.session.commit()
## RECORDING IT IN ANALYTICS
voted_item = Analytics(user_id=str(current_user),action=str('vote'),item_id=item_id)
db.session.add(voted_item)
db.session.commit()
I don't get any errors in my log files. When I hit vote I get the flask message that says It was successful. I am successfully logging the vote in a separate Analytics table, but for some reason its not recording it in the items table.
Ive tried a few things. I went back to sql statement to update the items virtual table, something like this:
sqlstr = "UPDATE items SET item_rank = item_rank +1 where id = {}".format(item_id)
crsr = conn.execute(sqlstr)
crsr.close()
But I was getting data base is locked errors, I assume because I was access db outside of Sqlalchemy orm...Any ideas? Because of a fts5, virtual table? going back and forth form sql to orm?

Related

SQLAlchemy Inserts dont get Saved

I Am trying to add some Rows to My Table with SQLAlchemy to my local SQLLite File. However the Rows dont get saved. I dont get any Primary Key Errors and i am also commiting my Inserts/Session.
What's really confusing me is that my Code works fine(My Inserts are Saved), when i am changing my Engine to Connect to a local postgresql-Server. I Am Calling the add_values Function from an other File.
def add_values(offers):
engine = create_engine("sqlite:///mydb.db",echo=True)
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
with Session.begin() as session:
list_of_offers =[]
for ele in offers:
offer = Offer(ele.id,ele.asin,ele.price,ele.currency,ele.condition,ele.seller,ele.datetime,ele.buyboxWinner,ele.numberOfSellers)
list_of_offers.append(offer)
session.add_all(list_of_offers)
session.commit()
I Tryed to Change my Database to postgresql-Server.

How do I query the table in sqlalchemy when I use the reflect parameter "only=[table_name]"?

I'm trying to build a flask web app that interfaces with a remote, pre-existing sql server database.
I have a successful connection, according to the output of echo='debug.'
I'm using the .reflect() "only" parameter because the database I'm connecting to has hundreds of tables. Without it, the entire database gets reflected and it runs way too slow.
engine = create_engine('mssql+pymssql://user:pass#server/db', echo='debug')
conn = engine.connect()
meta = MetaData(engine).reflect(schema='dbo', only=['Bookings'])
table = meta.tables['Bookings']
select_st = select([table]).where(
table.c.ID == 'id-1234')
res = conn.execute(select_st)
for _row in res:
print(_row)
The problem is that I'm getting the error:
table = meta.tables['Bookings']
AttributeError: 'NoneType' object has no attribute 'tables'
My guess is that .tables doesn't work with the subset 'Bookings' that I've passed it because .tables calls the meta object, which it believes should be a database, not a a table.
How can I get sqlalchemy features to work with that 'only' parameter? Again, I'm building a flask web app so the solution needs to be able to interface with Base.automap.
.reflect() does not return a value / returns None. So meta is None.
Try it like this please:
meta = MetaData(engine)
meta.reflect(schema='dbo', only=['Bookings'])
table = meta.tables['Bookings']

SQLite database not updating after inserting data from Flask python

I have an app route in a Flask web application, which under the "POST" method, takes data from a website form and is supposed to add an entry into a SQLite database (database.db) with that info in the table 'times'. The code runs without invoking any errors, but the database does not update with the added entry. The Flask app properly retrieves the info from the form. This is my code for connecting to the database and attempting to update it. Am I doing anything wrong?
connection=sqlite3.connect("database.db")
cursor=connection.cursor()
cursor.execute("INSERT INTO times (firstname, lastname, year, gender, event, minutes, seconds, milliseconds, date) VALUES(:first, :last, :year, :gender, :event, :minutes, :seconds, :milliseconds, :date)",
dict(first=first, last=last, year=year, gender=gender, event=event, minutes=minutes, seconds=seconds, milliseconds=milliseconds, date=date))
return redirect("/")
Missing connection.commit().
Please add that after the execute statement to save the entry in DB.

Reflection in SQLAlchemy doesn't work for MS SQL Server system tables?

I'm trying to reflect system table in MS SQL Server database:
from sqlalchemy import engine, create_engine, MetaData, Table
meta = MetaData()
url = engine.url.URL(
"mssql+pyodbc",
username=credentials["username"],
password=credentials["pswd"],
host=credentials["host"],
database=credentials["db"],
query=dict(driver="ODBC Driver 13 for SQL Server")
)
e = create_engine(url)
conn = e.connect()
tt = Table("objects", meta, autoload=True, autoload_with=e, schema="sys")
for c in tt.columns:
print(c.name)
At the end I get NoSuchTable error. I tried to reflect other system tables (sys.triggers, sys.sql_modules) - same results. With ordinary tables this code works normally, I can list columns and make other queries. Login which I use in my application has "db_owner" role, so it has enough permissions, and if I write something like this
for item in conn.execute("select * from sys.triggers"):
print(item)
it works fine.
What am I doing wrong? Is there any other way to work with data from system tables, besides executing raw sql and wrapping results in dataclasses etc.?
I was trying to reflect system views in MS SQL Server database. After adding echo='debug' to engine, I realized that, SQL Alchemy searches table and view metadata from INFORMATION_SCHEMA in MSSQL.
The system tables and views are not listed in INFORMATION_SCHEMA.TABLES OR INFORMATION_SCHEMA.VIEWS.
(I'm using SQLAlchemy version 1.3.5.)
When you add echo='debug' to your engine you can see the steps it's going through when talking to the database. In my case it sends out a query consisting of a left join of all_col_comments on all_tab_cols. In this query you'll see it uses owner = <schema value>.
I found that the system tables are owned by 'SYS' so by setting the schema to 'SYS' it will be able to find the system table just fine. A small code example to clarify where to set the schema:
table = db.Table('USER_SOURCE', metadata, schema='SYS', autoload=True, autoload_with=engine)

How to get string value of user id type <class 'str'> [facebook messenger bot]

I'm creating facebook messenger app that is text based but does not require user to write specific commands.
I'm using python 3.6 and django 1.11.
The app asks for user's input and updates the phase that user has accomplished to database.
This system requires me to save user id to database and I have done that already. It works.
Instead.. when I try to create a database query it won't return any rows if I use the data from user's messenger message.
Here's how I create a record to database:
feedback_object, created = Feedback.objects.update_or_create(
source_created_at=feedback_start_at,
user_id=message['sender']['id'],
defaults={
'message': message['message']['text'],
'phase': feedback['phase']
}
Here's query that I CAN get the data from database:
r = Feedback.objects.filter(user_id='12345').latest('source_created_at')
Here's query that DOES NOT work but I need something similar:
r = Feedback.objects.filter(user_id=message['sender']['id']).latest('source_created_at')
Query doesn't return anything.
Solution: I went around this all by doing temporary db record that I can fetch and use in comparison with the original post.

Categories

Resources