Created
April 23, 2017 12:56
-
-
Save x011/c7bda2bf36a8a1be3412889d3f23c1f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sqlite3 | |
import os.path | |
from datetime import datetime | |
csv_file = "csv_to_db.csv" | |
db_file = "csv_to_db.sqlite" | |
if not os.path.exists(db_file): | |
#id,mtype,competition,gender,team1,team2,venue,date | |
con = sqlite3.Connection(db_file,detect_types=sqlite3.PARSE_DECLTYPES) | |
cur = con.cursor() | |
cur.execute('CREATE TABLE "venues" ("id" int primary key, "mtype" text,' | |
' "competition" text, "gender" text, "team1" text, ' | |
'"team2" text, "venue" text, "venue_date" date);') | |
f = open(csv_file) | |
csv_reader = csv.reader(f, delimiter=',') | |
cur.executemany('INSERT INTO venues VALUES (?, ?, ?, ?, ?, ?, ?, ?)', csv_reader) | |
cur.close() | |
con.commit() | |
con.close() | |
f.close() | |
# Connecting to the database file | |
conn = sqlite3.connect(db_file,detect_types=sqlite3.PARSE_DECLTYPES) | |
c = conn.cursor() | |
start_date = "2017-02-19" | |
end_date = "2017-02-20" | |
c.execute("SELECT * FROM {table} WHERE mtype='{query}' AND venue_date BETWEEN date('{start_date}') AND date('{end_date}')".format(table="venues", query="ODM", start_date=start_date, end_date=end_date)) | |
all_rows = c.fetchall() | |
print(all_rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment