Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mysql.connector import connect
- from mysql.connector.errors import ProgrammingError
- from mysql.connector.errors import InterfaceError
- def connect_to_db(db_name):
- cnx = connect(user="root", password="", host="localhost", database=db_name)
- cnx.autocommit = True
- return cnx
- sql_create_table_product = """
- CREATE TABLE Product (
- id INT NOT NULL AUTO_INCREMENT,
- name CHAR(50),
- description VARCHAR(255),
- price FLOAT(5,2),
- PRIMARY KEY(id));
- """
- sql_create_table_shop_order = """
- CREATE TABLE Shop_order (
- id INT NOT NULL,
- description VARCHAR(255),
- PRIMARY KEY(id));
- """
- sql_insert_product = """
- INSERT INTO product
- VALUES('dlugopis','rozowy dlugopis', 2.8);
- """
- try:
- cnx = connect_to_db("exercises_db")
- cursor = cnx.cursor()
- # cursor.execute(sql_create_table_product)
- # cursor.execute(sql_create_table_shop_order)
- cursor.execute(sql_insert_product)
- print(cursor.lastrowid)
- cursor.execute(sql_insert_product)
- print(cursor.lastrowid)
- cursor.execute(sql_insert_product)
- print(cursor.lastrowid)
- cursor.close()
- cnx.close()
- except ProgrammingError as e:
- print(e)
- except InterfaceError as e:
- print(e)
Add Comment
Please, Sign In to add comment