Advertisement
DrAungWinHtut

fun1.py

Mar 6th, 2023
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 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. # Function to select data from a table
  24.  
  25.  
  26. def select_data(conn, table_name, columns="*", condition=None):
  27.     try:
  28.         cursor = conn.cursor()
  29.         if condition is None:
  30.             # query = "SELECT * FROM area_tb"
  31.             query = "SELECT {0} FROM {1}".format(
  32.                 columns,  # 0
  33.                 table_name
  34.             )
  35.             cursor.execute(query)
  36.         else:
  37.             query = "SELECT {0} FROM {1} WHERE {2}".format(
  38.                 columns,  # 0
  39.                 table_name,
  40.                 condition
  41.             )
  42.             cursor.execute(query)
  43.  
  44.         result = cursor.fetchall()
  45.         # print(data)
  46.         return result
  47.  
  48.     except Exception as e:
  49.         conn.rollback()
  50.         print("Error selecting data: ", e)
  51.  
  52.  
  53. def greeting(name):
  54.     print("Hello " + name)
  55.     print()
  56.     print()
  57.  
  58.  
  59. def area(l, w):
  60.     a = l * w
  61.     print('area = ', a)
  62.     columns = ('l', 'w', 'a')
  63.     values = (l, w, a)
  64.     insert_db(conn, 'area_tb', columns, values)
  65.     os.system('pause')
  66.     os.system('cls')
  67.     print()
  68.     print()
  69.  
  70.  
  71. def tempF2C(tf):
  72.     tc = ((5/9)*tf) - 32
  73.     print('tc = ', tc)
  74.     columns = ('tf', 'tc')
  75.     values = (tf, tc)
  76.     insert_db(conn, 'temp_tb', columns, values)
  77.     os.system('pause')
  78.     os.system('cls')
  79.     print()
  80.     print()
  81.  
  82.  
  83. def square(n):
  84.     square_n = n*n
  85.  
  86.     columns = ('n', 'square_n')
  87.     values = (n, square_n)
  88.     insert_db(conn, 'square_tb', columns, values)
  89.     os.system('pause')
  90.     os.system('cls')
  91.     print()
  92.     print()
  93.     return (square_n)
  94.  
  95.  
  96. def menu():
  97.     while True:
  98.         print('Menu')
  99.         print('=====')
  100.         print('1-calculate area: ')
  101.         print('2-calculate temperature: ')
  102.         print('3-calculate square: ')
  103.         print('4-show calculated area data')
  104.         print('0-Exit')
  105.         print()
  106.  
  107.         ans = input('Please choose (0,1,2,3): ')
  108.         if (ans == '1'):
  109.             l = eval(input('Please enter L: '))
  110.             w = eval(input('Please enter W: '))
  111.             area(l, w)
  112.  
  113.         elif (ans == '2'):
  114.             tf = eval(input('Please enter tf: '))
  115.             tempF2C(tf)
  116.  
  117.         elif (ans == '3'):
  118.             n = eval(input('Please enter n: '))
  119.             s = square(n)
  120.             print('square = ', s)
  121.             print()
  122.             print()
  123.  
  124.         elif (ans == '4'):
  125.             result = select_data(conn, "area_tb", "*", None)
  126.             print(' ID             L              W               A')
  127.             for row in result:
  128.                 print('{0:3d}    {1:10.2f}     {2:10.2f}      {3:10.2f}'.format(
  129.                     row[0], row[1], row[2], row[3]))
  130.  
  131.             os.system('pause')
  132.  
  133.         elif (ans == '0'):
  134.             exit()
  135.  
  136.  
  137. # Program Start here!
  138.  
  139. # test code ends here
  140. # name = input('What is your name: ')
  141. # greeting(name)
  142.  
  143. server = 'localhost'
  144. admin = 'root'
  145. pwd = ''
  146. dbname = 'calculation_db'
  147.  
  148. try:
  149.     conn = pymysql.connect(host=server,
  150.                            user=admin,
  151.                            password=pwd,
  152.                            database=dbname
  153.                            )
  154.  
  155. except Exception as e:
  156.     print('Cannot connect ot' + dbname + 'database')
  157.     os.system('pause')
  158.  
  159.  
  160. os.system('cls')
  161. print('Successfully connected to {} database'.format(dbname))
  162. print()
  163. menu()
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement