Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
connection python and SQL
#1
Hi python experts, Is possible to connect Python and SQL? In jupyter notebook or in Pycharm? Is possible start the code from SQL in Python? If Yes how?Thanks
Reply
#2
Could you elaborate? It's not clear what you ask. Of course it's possible to interact with a DB from/via python code
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

Reply
#3
ok i have created the code in PL/SQL developer where i download and edit the data, after that I save this data to excel and after that I start The code with model in python. I would like to improve this process and use only Python, because the SQL code still will the same and I would like to save my time. But I do not know how can i do this which programme i have to use. Thanks
Reply
#4
Still not sure if you want to rewrite some parts in python or just call PL/SQL from python.
Maybe have a look ate https://python-oracledb.readthedocs.io/e...ution.html
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

Reply
#5
I think it is a good idea to access the database using phpMyAdmin first. Make an SQL query and try it, to make sure that it works. When you know you have a query that works, then you can do it from Python.

I only do simple stuff. For me, pymysql works fine.

import pymysql 

def mysqlINSERT(name, email, comment): 
    conn = pymysql.connect( 
        host='127.0.0.1', 
        user='baby',  
        password = 'Taizhou', 
        db='babydb', 
        )        
    cur = conn.cursor() 
    # INSERT query 
    command = 'INSERT INTO agro_products (customer_id, product_name, product_class) VALUES (%s, %s, %s)'
    cur.execute(command, (name, email, comment))
    # without conn.commit() all you get is simulation, nothing is actually written
    conn.commit()
    # To close the connection 
    conn.close()
    return 'OK'

name = 'Use me'
email = '[email protected]'
comment = 'Hope this kills everything!'
doit = mysqlINSERT(name, email, comment)
Here is a query to return the column names of a given table:

Quote:SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'babydb' AND TABLE_NAME = 'agro_products';

Try it in phpMyAdmin first, using your database name and table name, of course. When you know it works, try it in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I don't know what is wrong (Python and SQL connection) shereen 3 371 Apr-01-2024, 08:56 AM
Last Post: Pedroski55
  No Internet connection when running a Python script basil_555 8 663 Mar-11-2024, 11:02 AM
Last Post: snippsat
  Connection LTspice-Python with PyLTSpice bartel90 0 358 Feb-05-2024, 11:46 AM
Last Post: bartel90
  Virtual Env changing mysql connection string in python Fredesetes 0 383 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 741 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Python MYSQL connection does not work after 1h idle zazas321 9 6,841 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  Serial connection connection issue Joni_Engr 15 8,100 Aug-30-2021, 04:46 PM
Last Post: deanhystad
  How can I make a plot representing connection relationship with python matplotlib? Berlintofind 1 1,966 May-08-2020, 09:27 PM
Last Post: jefsummers
  Connection DATABASE to Python MenThoLLt 3 2,442 Jan-17-2020, 10:35 PM
Last Post: DT2000
  Python Aspentech IP.21 connection MarcoWedel 0 1,988 Dec-17-2019, 10:02 PM
Last Post: MarcoWedel

Forum Jump:

User Panel Messages

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