Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
connection python and SQL
#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


Messages In This Thread
connection python and SQL - by dawid294 - Dec-11-2023, 09:28 AM
RE: connection python and SQL - by buran - Dec-11-2023, 09:31 AM
RE: connection python and SQL - by dawid294 - Dec-11-2023, 10:34 AM
RE: connection python and SQL - by buran - Dec-11-2023, 11:43 AM
RE: connection python and SQL - by Pedroski55 - Dec-12-2023, 08:22 AM

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