Skip to content
Advertisement

Tag: sql

How to execute raw SQL in Flask-SQLAlchemy app

How do you execute raw SQL in SQLAlchemy? I have a python web app that runs on flask and interfaces to the database through SQLAlchemy. I need a way to run the raw SQL. The query involves multiple table joins along with Inline views. I’ve tried: But I keep getting gateway errors. Answer Have you tried: or: Note that db.engine.execute()

How to check if table exists?

I’m using this function : I want to check if the table exists, how can I do it? I saw some examples using XXXX.execute(). What does it mean? Here is what I saw: I tried printing MY_T to see if it returns -1 for example but it just prints “select count (*)…… ” How can I check it? Answer Use

How to get month and year from Date field in sqlalchemy?

The result should be Date object Since the day cannot be ‘removed’, set it to say, 1st day of the month. Leaving only Month, Year Answer You can use following constructs to filter the Date column using either year or month: And group_by is also possible: Now I haven’t tested it, but I assume that this might be slow because

Retrieving Data from SQL Using pyodbc

I am trying to retrieve data from an SQL server using pyodbc and print it in a table using Python. However, I can only seem to retrieve the column name and the data type and stuff like that, not the actual data values in each row of the column. Basically I am trying to replicate an Excel sheet that retrieves

How to query database by id using SqlAlchemy?

I need to query a SQLAlchemy database by its id something similar to but for id. How do I do this? [Searching over Google and SO didn’t help] Answer Query has a get function that supports querying by the primary key of the table, which I assume that id is. For example, to query for an object with ID of

Select NULL Values in SQLAlchemy

Here’s my (PostgreSQL) table — I want to select all people that are not known to be married, i.e., including those with NULL marriage_status. This does not work — Of course this does — The problem is that I’m accessing it from SQLAlchemy with — which gets translated to — And does not work — neither does — How should

Python SQL query string formatting

I’m trying to find the best way to format an sql query string. When I’m debugging my application I’d like to log to file all the sql query strings, and it is important that the string is properly formated. Option 1 This is good for printing the sql string. It is not a good solution if the string is long

Aggregating save()s in Django?

I’m using Django with an sqlite backend, and write performance is a problem. I may graduate to a “proper” db at some stage, but for the moment I’m stuck with sqlite. I think that my write performance problems are probably related to the fact that I’m creating a large number of rows, and presumably each time I save() one it’s

Python dicts in sqlalchemy

I would like to load/save a dict to/from my sqlite DB, but am having some problems figuring out a simple way to do it. I don’t really need to be able to filter, etc., based on the contents so a simple conversion to/from string is fine. The next-best thing would be foreign keys. Please don’t post links to huge examples,

Advertisement