Guest User

Untitled

a guest
Oct 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. from mysql.connector import connect
  2.  
  3. # Function that connects to db
  4. def connect_to_db(db_name):
  5. cnx = connect(user="root", password="", host="localhost", database=db_name)
  6. cnx.autocommit = True
  7. return cnx
  8.  
  9. # Function that creates connection and inserts stuff from parameter
  10. def insert_into_product(slownik):
  11. cnx = connect_to_db("exercises_db")
  12. cur = cnx.cursor()
  13. sql = """INSERT INTO product(name, description, price)
  14. VALUES('{}', '{}', {})""".format(slownik["name"],
  15. slownik["description"],
  16. slownik["price"])
  17.  
  18. cur.execute(sql)
  19. cur.close()
  20. cnx.close()
  21.  
  22. # Prepare data to be inserted
  23. slow = {"name":"dlugopis", "description":"fajny", "price": 2.5}
  24.  
  25. # Call function that inserts stuff
  26. insert_into_product(slow)
  27. print("Stuff inserted")
Add Comment
Please, Sign In to add comment