maroph

db_tracking_cards.py

Nov 16th, 2019
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import sqlite3
  2. from random import randint
  3. from time import time
  4.  
  5. conn = sqlite3.connect("computer_cards.db")
  6.  
  7.  
  8. def get_number_of_cards():
  9.     """Returns the total number of rows in table computer"""
  10.     # not used in this sample but very useful in general
  11.     cursor = conn.cursor()
  12.     sql = "SELECT COUNT(*) FROM 'computer'"
  13.     print("SQL:>>>>>" + sql + "<<<<<")
  14.     cursor.execute(sql)
  15.     count = cursor.fetchone()
  16.     cursor.close()
  17.     print("SQL: number of rows in table computer: {}".format(count[0]))
  18.     return count[0]
  19.  
  20.  
  21. def get_number_of_picked_cards():
  22.     """Returns the total number of rows in table picked"""
  23.     # not used in this sample but very useful in general
  24.     cursor = conn.cursor()
  25.     sql = "SELECT COUNT(*) FROM 'picked'"
  26.     print("SQL:>>>>>" + sql + "<<<<<")
  27.     cursor.execute(sql)
  28.     count = cursor.fetchone()
  29.     cursor.close()
  30.     print("SQL: number of rows in table picked: {}".format(count[0]))
  31.     return count[0]
  32.  
  33.  
  34. def read_all_cards():
  35.     sql = "SELECT * FROM computer"
  36.     print("SQL:>>>>>" + sql + "<<<<<")
  37.     result = conn.execute(sql)
  38.     return result.fetchall()
  39.  
  40.  
  41. def insert_picked(name):
  42.     sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  43.     print("SQL:>>>>>" + sql + "<<<<<")
  44.     conn.execute(sql)
  45.     conn.commit()
  46.  
  47.  
  48. def read_last_picked():
  49.     sql = "SELECT * FROM picked ORDER BY time DESC"
  50.     print("SQL:>>>>>" + sql + "<<<<<")
  51.     result = conn.execute(sql)
  52.     return result.fetchone()
  53.  
  54.  
  55. def dump_table_picked():
  56.     sql = "SELECT * FROM picked ORDER BY time DESC"
  57.     print("SQL:>>>>>" + sql + "<<<<<")
  58.     result = conn.execute(sql)
  59.     picked_cards = result.fetchall()
  60.     print("cards in table picked")
  61.     print("---------------------")
  62.     for card in picked_cards:
  63.         print(card)
  64.     print("---------------------")
  65.  
  66. def delete_content_of_table_picked():
  67.     sql = "DELETE FROM picked"
  68.     print("SQL:>>>>>" + sql + "<<<<<")
  69.     cursor = conn.cursor()
  70.     cursor.execute(sql)
  71.     print("SQL: number of deleted rows: {}".format(cursor.rowcount))
  72.  
  73.  
  74. def pick_card():
  75.     last_picked_card = read_last_picked()
  76.     print("last_picked_card:", last_picked_card)
  77.  
  78.     random_card = cards[randint(0, len(cards) - 1)]
  79.  
  80.     if last_picked_card is not None:
  81.         # the if above fixes a bug when table picked is empty
  82.         while random_card[0] == last_picked_card[0]:
  83.             random_card = cards[randint(0, len(cards) - 1)]
  84.  
  85.     insert_picked(random_card[0])
  86.     return random_card
  87.  
  88.  
  89. cards = read_all_cards()
  90. print("number of available cards: {}".format(get_number_of_cards()))
  91.  
  92. picked_cards = get_number_of_picked_cards()
  93. if picked_cards > 10:
  94.     print("truncate table picked")
  95.     delete_content_of_table_picked()
  96.  
  97. resp = "y"
  98. while resp == "y":
  99.     print("picked card:", pick_card())
  100.     dump_table_picked()
  101.     print("")
  102.     resp = input("pick another card?[y/n]:")
  103.  
  104. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment