Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- # pip install mysql-connector-python
- conn = mysql.connector.connect(
- host='localhost',
- user='root',
- password='',
- database='greenhackers'
- )
- cursor = conn.cursor()
- def insert_data(name,age,salary,address):
- insert_query = "insert into customers (name,age,salary,address) values (%s,%s,%s,%s)"
- idata = (name,age,salary,address)
- cursor.execute(insert_query,idata)
- conn.commit()
- def update_data(name,age,salary,address,id):
- update_query = "update customers set name=%s, age=%s, salary=%s,address=%s where id= %s"
- udata = (name,age,salary,address,id)
- cursor.execute(update_query,udata)
- conn.commit()
- def delete_data(id):
- delete_query = "delete from customers where id = %s"
- ddata = (id,)
- cursor.execute(delete_query,ddata)
- conn.commit()
- def show_data(table):
- select_query = f'SELECT * FROM {table}'
- cursor.execute(select_query)
- result = cursor.fetchall()
- # print(result)
- for row in result:
- print(row)
- def create_table():
- create_table_query = "create table if not exists horoscope (id int auto_increment, scope varchar(100) not null, primary key(id))"
- cursor.execute(create_table_query)
- print("Table is created successfully.")
- def insert_horoscope(horoscope):
- insert_query = 'insert into horoscope (scope) values (%s)'
- idata = (horoscope,)
- print(insert_query)
- cursor.execute(insert_query,idata)
- conn.commit()
- # Actual Program Start Here
- #insert_data('koko',22,2000,'malaysia')
- #update_data('toe toe',200,20000,'USA',16)
- #delete_data(16)
- #create_table()
- #insert_horoscope("Gemini: Today is a day for new beginnings. Embrace change and explore new opportunities that come your way. Trust your instincts and follow your heart.")
- #insert_horoscope("Leo: Your confidence is shining bright today. Use your charm and charisma to make a positive impact on those around you. Don't be afraid to take the lead.")
- #insert_horoscope("Taurus: It's time to focus on your finances. Take a closer look at your budget and find ways to save money. A small investment now can lead to great rewards in the future.")
- #insert_horoscope("Virgo: Pay attention to your health and well-being today. Take some time to relax and recharge. A little self-care can go a long way in boosting your productivity.")
- #insert_horoscope("Aries: Your determination and drive are at an all-time high. Channel your energy into pursuing your goals and don't let anything distract you. Success is within your reach.")
- show_data('horoscope')
- cursor.close()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement