Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mysql.connector import connect
- # Function that connects to db
- def connect_to_db(db_name):
- cnx = connect(user="root", password="", host="localhost", database=db_name)
- cnx.autocommit = True
- return cnx
- # Function that creates connection and inserts stuff from parameter
- def insert_into_product(slownik):
- cnx = connect_to_db("exercises_db")
- cur = cnx.cursor()
- sql = """INSERT INTO product(name, description, price)
- VALUES('{}', '{}', {})""".format(slownik["name"],
- slownik["description"],
- slownik["price"])
- cur.execute(sql)
- cur.close()
- cnx.close()
- # Prepare data to be inserted
- slow = {"name":"dlugopis", "description":"fajny", "price": 2.5}
- # Call function that inserts stuff
- insert_into_product(slow)
- print("Stuff inserted")
Add Comment
Please, Sign In to add comment