Advertisement
DrAungWinHtut

calculation_db.py

Feb 28th, 2023
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1.  
  2. # Import all
  3. import pymysql
  4. import os
  5.  
  6.  
  7. def insert_db(conn, tb_name, columns, values):
  8.     try:
  9.         cursor = conn.cursor()
  10.         # sql = "INSERT INTO area_tb (l,w,a) VALUES (%s,%s,%s)"
  11.         sql = "INSERT INTO {0} ({1}) VALUES ({2})".format(
  12.             tb_name,  # 0
  13.             ", ".join(columns),  # 1
  14.             ", ".join(["%s"] * len(columns))  # 2
  15.         )
  16.         cursor.execute(sql, values)
  17.         conn.commit()
  18.         print('Data insert complete')
  19.     except Exception as e:
  20.         conn.rollback()
  21.         print('Error inserting data: ', e)
  22.  
  23.  
  24. def greeting(name):
  25.     print("Hello " + name)
  26.     print()
  27.     print()
  28.  
  29.  
  30. def area(l, w):
  31.     a = l * w
  32.     print('area = ', a)
  33.     columns = ('l', 'w', 'a')
  34.     values = (l, w, a)
  35.     insert_db(conn, 'area_tb', columns, values)
  36.     os.system('pause')
  37.     os.system('cls')
  38.     print()
  39.     print()
  40.  
  41.  
  42. def tempF2C(tf):
  43.     tc = ((5/9)*tf) - 32
  44.     print('tc = ', tc)
  45.     columns = ('tf', 'tc')
  46.     values = (tf, tc)
  47.     insert_db(conn, 'temp_tb', columns, values)
  48.     os.system('pause')
  49.     os.system('cls')
  50.     print()
  51.     print()
  52.  
  53.  
  54. def square(n):
  55.     square_n = n*n
  56.  
  57.     columns = ('n', 'square_n')
  58.     values = (n, square_n)
  59.     insert_db(conn, 'square_tb', columns, values)
  60.     os.system('pause')
  61.     os.system('cls')
  62.     print()
  63.     print()
  64.     return (square_n)
  65.  
  66.  
  67. def menu():
  68.     while True:
  69.         print('Menu')
  70.         print('=====')
  71.         print('1-calculate area: ')
  72.         print('2-calculate temperature: ')
  73.         print('3-calculate square: ')
  74.         print('0-Exit')
  75.         print()
  76.  
  77.         ans = input('Please choose (0,1,2,3): ')
  78.         if (ans == '1'):
  79.             l = eval(input('Please enter L: '))
  80.             w = eval(input('Please enter W: '))
  81.             area(l, w)
  82.  
  83.         elif (ans == '2'):
  84.             tf = eval(input('Please enter tf: '))
  85.             tempF2C(tf)
  86.  
  87.         elif (ans == '3'):
  88.             n = eval(input('Please enter n: '))
  89.             s = square(n)
  90.             print('square = ', s)
  91.             print()
  92.             print()
  93.  
  94.         elif (ans == '0'):
  95.             exit()
  96.  
  97.  
  98. # Program Start here!
  99.  
  100. # test code ends here
  101. # name = input('What is your name: ')
  102. # greeting(name)
  103.  
  104. server = 'localhost'
  105. admin = 'root'
  106. pwd = ''
  107. dbname = 'calculation_db'
  108.  
  109. try:
  110.     conn = pymysql.connect(host=server,
  111.                            user=admin,
  112.                            password=pwd,
  113.                            database=dbname
  114.                            )
  115.     print('Successfully connected to {} database'.format(dbname))
  116. except Exception as e:
  117.     print('Cannot connect ot' + dbname + 'database')
  118.  
  119. os.system('pause')
  120. os.system('cls')
  121. menu()
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement