Uploaded by Shivendra Singh

account management

advertisement
import mysql.connector
import pandas as pd
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
# here, the 'db' is the variable that contains your database connection information
class accountManager:
def connection(self):
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
#can be used try except clause for successful testing of connection
#CREATE TABLE METHOD
def createTable(self):
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
cursor=db.cursor()
#cursor.execute("drop table if exists")
cursor.execute('''create table if not exists ACCOUNT (
Account_ID int PRIMARY KEY,
Account_Name varchar(250),
Account_Size int,
Account_Duration int,
Account_Budget float,
Status char(10))''')
db.close()
#INSERTING VALUES INTO THE TABLE
def insert(self):
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
n=int(input("No of rows to insert: "))
for i in range(n):
Id=int(input())
name=input()
size=int(input())
duration=int(input())
budget=float(input())
status=input()
cursor=db.cursor()
cursor.execute("""INSERT INTO ACCOUNT
(Account_ID,Account_Name,Account_Size,Account_Duration,Account_Budget,Status)
VALUES(%s,%s,%s,%s,%s,%s)""", (Id,name,size,duration,budget,status))
db.commit()
db.close()
#FETCHING ALL RECORDS FROM ACCOUNT TABLE
def viewAll(self):
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
Cursor=db.cursor()
Cursor.execute("select * from Account")
res=Cursor.fetchall()
for i in res:
print(i)
#UPDATING THE STATUS OF RECORD (BY INPUTTING ACCOUNT_ID)
def updateStatus(self):
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
Cursor=db.cursor()
Id=int(input("enter id: "))
status=input("enter status")
Cursor.execute("update account set status=%s where Account_Id=%s ",(status,Id))
db.commit()
db.close()
#DELETING LAST RECORD
def delete(self):
db=mysql.connector.connect(username="root",password="jhanguman",database="test")
cursor=db.cursor()
cursor.execute("delete from account order by Account_Id desc limit 1 ")
db.commit()
db.close()
account=accountManager() #CREATION OF CLASS OBJECT
print("Account Management System")
print("by Aniket Singh")
print("1:connection 2.create table 3. insert no of rows 4. View all Accounts 5. Update Status by id 6.
delete table 7. exit and save in csv ")
choice=int(input("enter choice...."))
#PYTHON VERSION OF SWITCH CASE
if(choice==1):
account.connection()
elif(choice==2):
account.createTable()
elif(choice==3):
account.insert()
elif(choice==4):
account.viewAll()
elif(choice==5):
account.updateStatus()
elif(choice==6):
account.delete()
elif(choice==7):
# db=mysql.connector.connect(username="root",password="jhanguman",database="test")
sql_query = pd.read_sql_query('''
select * from account order by Account_Id
'''
,db)
#CALLING PANDAS FOR WRITING INTO CSV
df = pd.DataFrame(sql_query)
df.to_csv (r'C:\Users\Anike\Desktop\account.csv', index = False)
exit(7)
db.close()
# In[ ]:
# In[ ]:
Download