Advertisement
JAS_Software

SQL pick card

May 16th, 2021
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sqlite3
  2. from random import randint
  3. from time import time
  4. import sys
  5.  
  6. def getConnection():
  7.     #create connection to the database
  8.     print('Creating connection to the Database')
  9.     conn = ''
  10.     try:
  11.         conn = sqlite3.connect('computer_cards.db')
  12.     except Exception as ex:
  13.         print('Failed to establish connection to database - {}'.format(ex))
  14.         conn = ''
  15.     return conn
  16.  
  17. def closeConnection(conn):
  18.     #close connection to database
  19.     print('Closing connection to the Database')
  20.     try:
  21.         conn.close()
  22.     except Exception as ex:
  23.         print('Error closing connection {}'.format(ex))
  24.  
  25. def clearTable(conn):
  26.     #clear all records from the picked table
  27.     try:
  28.         sql = "DELETE FROM picked"        
  29.         cursor = conn.execute(sql)
  30.         conn.commit()
  31.         print('Rows deleted - {}'.format(cursor.rowcount))
  32.     except Exception as ex:
  33.         print('Failed to clear table - {}'.format(ex))
  34.        
  35. def getCards(conn):
  36.     #create list of all card names from the computer table
  37.     try:
  38.         result = conn.execute("SELECT name FROM computer")
  39.         return result.fetchall()
  40.     except Exception as ex:
  41.         print('Failed to get table - {}'.format(ex))
  42.         return ''
  43.        
  44. def displayRecords(conn):
  45.     cursor = conn.execute('SELECT * FROM picked')
  46.     records = cursor.fetchall()
  47.     for record in records:
  48.         print(record)
  49.        
  50. def pickAnotherCard(conn):
  51.     response = input('Pick Another Card? ').strip().upper()
  52.     if response == 'D':
  53.         displayRecords(conn)
  54.         response = 'Y'
  55.     return (response == 'Y')
  56.  
  57. def addCard(conn,name):
  58.     #add card to the picked table
  59.     try:
  60.         sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name,time())        
  61.         cursor = conn.execute(sql)
  62.         conn.commit()
  63.         print('Added {}'.format(name))
  64.     except Exception as ex:
  65.         print('Failed to enter card {} - {}'.format(name, ex))
  66.  
  67. def getLastCard(conn):
  68.     try:
  69.         result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  70.         return result.fetchone()[0]
  71.     except Exception as ex:
  72.         print('Error getting last card picked, table empty - {}'.format(ex))
  73.         return ''
  74.    
  75.    
  76. def mainLoop(conn):
  77.     #pick random cards and add to picked table
  78.     pickCard = True
  79.     while pickCard:
  80.         cards = getCards(conn)
  81.         if cards != '':
  82.             lastCard = getLastCard(conn)
  83.             random_card = cards[randint(0, len(cards) - 1)][0]
  84.             while lastCard == random_card:
  85.                 print('LOOPING')
  86.                 random_card = cards[randint(0, len(cards) - 1)][0]                
  87.             addCard(conn,random_card)
  88.             pickCard = pickAnotherCard(conn)
  89.         else:
  90.             pickCard = False
  91.  
  92.  
  93. #Main Program
  94. conn = getConnection()
  95. if conn != '':
  96.     clearTable(conn)
  97.     mainLoop(conn)
  98.     displayRecords(conn)
  99.     closeConnection(conn)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement