diff options
author | Jackson Taylor <jtaylor@classicalconversations.com> | 2021-11-01 14:05:10 -0400 |
---|---|---|
committer | Jackson Taylor <jtaylor@classicalconversations.com> | 2021-11-01 14:05:10 -0400 |
commit | d80ea2862f4e0a95b5c1e115770a1a9b51a7caed (patch) | |
tree | 1d6521f43312c13be26eed0008a507eaad0dc355 | |
parent | f27df26e24e300bd125cce2953010706579bd64a (diff) |
Connect to db and insert times from csv
Need to come back and handler for SQL injections.
Currently will fail if run with an existing db file
-rwxr-xr-x | jacsr | 33 |
1 files changed, 29 insertions, 4 deletions
@@ -1,14 +1,39 @@ #!/usr/bin/python3 import csv +import sqlite3 RESULT_FILE = './cstimer.csv' +DATABASE_FILE = 'solves.db' -def main(): - with open(RESULT_FILE) as f: + +def create_solve_table(connection): + cursor = connection.cursor() + statement = """ +CREATE TABLE solves (number integer primary key autoincrement, time datetime) + """ + 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: - print(line) + # TODO: this is dangerous for sql injections. + cursor.execute( + 'INSERT into solves (time) VALUES (' + line['Time'] + ')') + + cursor.close() if __name__ == "__main__": - main() + connection = sqlite3.connect(DATABASE_FILE) + + create_solve_table(connection) + + insert_times_into_db(connection, RESULT_FILE) + + connection.commit() + connection.close() |