· 2 years ago · Jun 11, 2023, 07:00 PM
1import mysql.connector
2# pip install mysql-connector-python
3conn = mysql.connector.connect(
4 host='localhost',
5 user='root',
6 password='',
7 database='greenhackers'
8)
9
10cursor = conn.cursor()
11
12def insert_data(name,age,salary,address):
13 insert_query = "insert into customers (name,age,salary,address) values (%s,%s,%s,%s)"
14 idata = (name,age,salary,address)
15 cursor.execute(insert_query,idata)
16 conn.commit()
17
18def update_data(name,age,salary,address,id):
19 update_query = "update customers set name=%s, age=%s, salary=%s,address=%s where id= %s"
20 udata = (name,age,salary,address,id)
21 cursor.execute(update_query,udata)
22 conn.commit()
23
24def delete_data(id):
25 delete_query = "delete from customers where id = %s"
26 ddata = (id,)
27 cursor.execute(delete_query,ddata)
28 conn.commit()
29
30def show_data(table):
31 select_query = f'SELECT * FROM {table}'
32 cursor.execute(select_query)
33 result = cursor.fetchall()
34 # print(result)
35 for row in result:
36 print(row)
37
38
39def create_table():
40 create_table_query = "create table if not exists horoscope (id int auto_increment, scope varchar(100) not null, primary key(id))"
41 cursor.execute(create_table_query)
42 print("Table is created successfully.")
43
44
45def insert_horoscope(horoscope):
46 insert_query = 'insert into horoscope (scope) values (%s)'
47 idata = (horoscope,)
48 print(insert_query)
49 cursor.execute(insert_query,idata)
50 conn.commit()
51
52# Actual Program Start Here
53
54#insert_data('koko',22,2000,'malaysia')
55#update_data('toe toe',200,20000,'USA',16)
56#delete_data(16)
57
58#create_table()
59#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.")
60#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.")
61#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.")
62#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.")
63#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.")
64
65show_data('horoscope')
66
67
68
69
70
71
72cursor.close()
73conn.close()