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
Sad Selenium update broke python code genericusername12414 1 177 Mar-16-2024, 07:33 PM
Last Post: snippsat
  Why can I not see the code correctly in Python IDLE.? Trump 7 434 Mar-14-2024, 09:05 PM
Last Post: deanhystad
  Algorithm for extracting comments from Python source code Pavel1982 6 412 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Python best library for Excel reports & review of existing code MasterOfDestr 4 495 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
Lightbulb python code debuging yunus 1 286 Feb-11-2024, 03:48 PM
Last Post: deanhystad
  Python code to set column width 1418 11 940 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python code for alignment and font size 1418 0 272 Jan-14-2024, 03:56 AM
Last Post: 1418
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 872 Nov-15-2023, 06:56 PM
Last Post: jst
  How to run detectron2, as python embedded code in C++, on GPU? hassaniqbal931 3 1,038 Nov-02-2023, 04:45 PM
Last Post: blabling2
  how include a python code in notpad++ plugin akbarza 2 579 Sep-25-2023, 08:25 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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