Python Forum
Converting SQL Code To Python Code Query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting SQL Code To Python Code Query
#1
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
#2
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
#3
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
#4
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
#5
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
#6
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.
#7
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
#8
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#9
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
#10
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs



Possibly Related Threads…
Thread Author Replies Views Last Post
  python code not running Azdaghost 1 409 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  Mathematica code vs Python MiloChocolatito 0 790 Mar-31-2025, 06:32 AM
Last Post: MiloChocolatito
  I am getting an IndentError on my python code in VS Code and i dont know why jcardenas1980 11 5,962 Mar-22-2025, 09:49 AM
Last Post: Pedroski55
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 371 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  How do i run python code? smattiko83 5 1,086 Mar-06-2025, 09:20 PM
Last Post: ArchieLinux
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 647 Feb-13-2025, 06:13 PM
Last Post: lminc123
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 4,439 Nov-27-2024, 02:20 PM
Last Post: menator01
  CLI to python code azxo1 11 2,527 Oct-21-2024, 08:32 AM
Last Post: azxo1
  PC why serial code = converting to ascii ? trix 11 2,895 Oct-12-2024, 11:10 PM
Last Post: deanhystad
  Algorithm for extracting comments from Python source code Pavel1982 7 3,240 Aug-28-2024, 02:50 AM
Last Post: timothyferriss

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020