Guest User

Untitled

a guest
Oct 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. from mysql.connector import connect
  2. from mysql.connector.errors import ProgrammingError
  3. from mysql.connector.errors import InterfaceError
  4.  
  5. def connect_to_db(db_name):
  6. cnx = connect(user="root", password="", host="localhost", database=db_name)
  7. cnx.autocommit = True
  8. return cnx
  9.  
  10. sql_create_table_product = """
  11. CREATE TABLE Product (
  12. id INT NOT NULL AUTO_INCREMENT,
  13. name CHAR(50),
  14. description VARCHAR(255),
  15. price FLOAT(5,2),
  16. PRIMARY KEY(id));
  17. """
  18. sql_create_table_shop_order = """
  19. CREATE TABLE Shop_order (
  20. id INT NOT NULL,
  21. description VARCHAR(255),
  22. PRIMARY KEY(id));
  23. """
  24.  
  25. sql_insert_product = """
  26. INSERT INTO product
  27. VALUES('dlugopis','rozowy dlugopis', 2.8);
  28. """
  29.  
  30. try:
  31. cnx = connect_to_db("exercises_db")
  32. cursor = cnx.cursor()
  33. # cursor.execute(sql_create_table_product)
  34. # cursor.execute(sql_create_table_shop_order)
  35. cursor.execute(sql_insert_product)
  36. print(cursor.lastrowid)
  37. cursor.execute(sql_insert_product)
  38. print(cursor.lastrowid)
  39. cursor.execute(sql_insert_product)
  40. print(cursor.lastrowid)
  41. cursor.close()
  42. cnx.close()
  43. except ProgrammingError as e:
  44. print(e)
  45. except InterfaceError as e:
  46. print(e)
Add Comment
Please, Sign In to add comment