Python Forum

Full Version: Converting SQL Code To Python Code Query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi there,

Is there any Python Utility available on the Internet, that can convert SQL Code, to Python Code ?

Here is the SQL Code, I would like converting to Python Code :-

SELECT * INTO #Temp4
FROM
(
SELECT
tt5.[Venue],
tt5.[BID],
MIN(tt1.[Date]) AS [Date],
tt5.[Display / Flypast],
SUM(tt1.[Duration]) AS [Duration (mins)],
(SELECT DISTINCT '; ' + t5.[Aircraft Combined]
	FROM #Temp5 t5
	WHERE tt5.[Venue] = t5.[Venue]
	AND tt5.[BID] = t5.[BID]
	AND tt5.[Display / Flypast] = t5.[Display / Flypast]
	FOR XML PATH ('')) [Aircraft Combined],
SUM(tt5.[Dakota and Fighters in Same Slot?]) AS [Dakota and Fighters in Same Slot?]
Any info and help would be appreciated

Regards

Eddie Winch
You can use sqlalchemy I just wrote a simple SqlAlchemy tutorial (last week)
It makes SQL easy
tutorial: https://python-forum.io/Thread-SqlAlchem...-Data-Load
Many thanks for your information Larz60+, it's much appreciated. I can't work exactly, how to do a conversion with SQLAlchemy ? To get a resulting Output, in Python Code equivalent ?

I mean what would I have to type, in the Python, in my case 3.7 Shell ?

I have looked at some documentation, but I am not completely sure, of what to do.

I mean, doing the usual thing of importing, in this case SQLAlchemy, what else would I have to type, i.e. using the SQL Code, I provide in this Thread ?

Sorry about my lack of understanding.

Regards

Eddie Winch
well in python, you would:
  • Open a database link
  • create a cursor
  • create a string containing the query (using proper security measures, so as to avoid sql injection)
  • execute the string within the cursor
  • commit your changes
  • close the database link.
  • Learn how to do all of above, suggest: https://www.python-course.eu/sql_python.php
Alternative: Learn SqlAlchemy
Thanks for that info Larz60+, I see you can't simply, have SQL code translated into Python Code, using SQLAlchemy ?

Code has to be executed, as part of querying a database etc. Because at the moment, I only want, direct translation of that SQL Code, to Python Code.

Eddie
Quote: I see you can't simply, have SQL code translated into Python Code, using SQLAlchemy?

You need to define a model (which is a class the defines the table columns, indexes, etc.), quite simple, see tutorial
then to access code, create an SqlAlchemy session and run the query.
The model should also include common database methods that you would create, for example queries which you use over and over
as you class grows, your database can become more and more of a black box.

the tutorial will take you less time than you've already spent on this thread.
Hi Larz60+,

I simply wan't the SQL Code, I provide in this Thread, converted to Python Code.

Are there any Converters, i.e. SQL Code to Python/Pandas Code Utilities on the Internet ?

If you mean in your replies, that I can do that with, the SQLAlchemy Utility.

Could you tell me exactly, what I should type to achieve that ?

Sorry I am confused, about what I should type, even with the Tutorial you provide ?

Can you help me, if that is okay ?

Regards

Eddie Winch
You can use ORM like SQLAlchemy as suggested by Larz, but you can also use directly the SQL query. There are different packages/drivers to ork with different databases. What DB do you work on?
Hi buran,

I am using the following Excel File :-

http://web.archive.org/web/2009080507022...6AA506.xls

My sister is writing a Code, for me in SQL Code, to filter the Data, as I would like, it to be displayed.

I asked questions how to finish the Code, in following Thread, but nobody has replied to my latest Posts on Page 3, of that Thread unfortunately, for a Python Pandas Code I have. So I would like a conversion of that SQL Code, to Python/Pandas Code, I provide in this Thread, to help me solve the issues myself, here is the Thread Link, I am refering to :-

https://python-forum.io/Thread-Filtering...cal-Values

Regards

Eddie Winch
if you are not getting the desired response, obviously there is something unclear about your request - either it lack information or is too broad. In any case what you try to do here is example of XY problem and won't help - you ask something, that is not your actual problem.
The best approach would be to keep the discussion in the original thread.
Pages: 1 2