#!/usr/bin/python3 import csv import sqlite3 import sys # TODO: Parameterize with getopt. These should be fine defaults. RESULT_FILE = './cstimer.csv' DATABASE_FILE = 'solves.db' def create_solve_table(connection): cursor = connection.cursor() # TODO: Not sure what to do about DNF (Did Not Finish) solves statement = """ CREATE TABLE solves ( number integer primary key autoincrement, time datetime, scramble text ) """ cursor.execute(statement) cursor.close() def insert_times_into_db(connection, result_file): cursor = connection.cursor() with open(result_file) as f: csv_file = csv.DictReader(f, delimiter=';') for line in csv_file: # TODO: Add date, comment, etc. fields from CsTimer csv cursor.execute("INSERT into solves (time, scramble) VALUES (?, ?)", (line['Time'], line['Scramble'])) cursor.close() def read_times_from_database(connection): cursor = connection.cursor() rows = cursor.execute('SELECT * from solves').fetchall() print(rows) cursor.close() def usage(): print(""" jacsr - Jackson's Awesome Cube Statistics Recorder -h - show this help message -s - Setup database with the tables """) if __name__ == "__main__": setup_db = False # TODO: Use getopt instead if len(sys.argv) > 1: if sys.argv[1] == '-s': setup_db = True else: usage() sys.exit() connection = sqlite3.connect(DATABASE_FILE) if setup_db: create_solve_table(connection) insert_times_into_db(connection, RESULT_FILE) read_times_from_database(connection) connection.commit() connection.close()