Sep-23-2023, 08:16 AM
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.
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)