Python Forum
Sending data from the form to DB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending data from the form to DB
#3
I never used pymysql to write to a db before.

You need a conn.commit() or it will only simulate writing, not actually write anything.

Also, if you are not writing to all columns of the database table, the columns you don't write to will need default values, or you get an error.

import pymysql

def mysqlINSERT(name, email, comment): 
    conn = pymysql.connect( 
        host='127.0.0.1', 
        user='baby',  
        password = 'asecret', 
        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 = 'Big Dealer'
email = '[email protected]'
comment = 'Hope this works!'
doit = mysqlINSERT(name, email, comment)
If you are writing to all columns of the database table, you do not need to mention the column names, just the VALUES, so the command will look like this:

Quote:INSERT INTO agro_products VALUES (%s, %s, %s,%s, %s)
Reply


Messages In This Thread
Sending data from the form to DB - by quisatz - Sep-22-2023, 05:40 PM
RE: Sending data from the form to DB - by Pedroski55 - Sep-23-2023, 08:16 AM
RE: Sending data from the form to DB - by janeik - Sep-24-2023, 10:42 PM
RE: Sending data from the form to DB - by quisatz - Sep-26-2023, 06:50 PM
RE: Sending data from the form to DB - by quisatz - Sep-27-2023, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to request image from FORM Data usman 0 1,080 Aug-18-2022, 06:23 PM
Last Post: usman
  Help Sending Socket Data snippyro 0 1,126 Sep-23-2021, 01:52 AM
Last Post: snippyro
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 4,079 Apr-16-2021, 07:49 PM
Last Post: apollo
  Problem: Retrieving Form data PythonDev 3 3,257 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  how to parse multipart/form-data for xls or jpeg stream into python code and store v71017 0 3,461 Mar-20-2018, 01:09 PM
Last Post: v71017
  parsing mutipart form data in Lambda v71017 3 7,705 Mar-08-2018, 01:17 PM
Last Post: snippsat
  sending data to second python script Cyberfly 1 3,256 Jan-29-2018, 10:09 AM
Last Post: Cyberfly
  sending data from my raspberry pi to my website mohitsangavikar 2 18,045 Sep-05-2017, 06:55 PM
Last Post: wrybread
  binary data in source code form Skaperen 4 5,062 Jun-02-2017, 02:21 AM
Last Post: Skaperen
  sending data to thread kiyoshi7 1 39,839 Apr-22-2017, 05:21 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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