Let's write the DB on a file already...
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys, re, string, sqlite3
|
import sys, re, string, sqlite3, os.path
|
||||||
|
|
||||||
#
|
#
|
||||||
# The relational database of this problem consists of 3 tables:
|
# The relational database of this problem consists of 3 tables:
|
||||||
@@ -52,18 +52,18 @@ def load_file_into_database(path_to_file, connection):
|
|||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
#
|
#
|
||||||
# The main function
|
# Create if it doesn't exist
|
||||||
#
|
#
|
||||||
connection = sqlite3.connect(':memory:')
|
if not os.path.isfile('tf.db'):
|
||||||
create_db_schema(connection)
|
with sqlite3.connect('tf.db') as connection:
|
||||||
load_file_into_database(sys.argv[1], connection)
|
create_db_schema(connection)
|
||||||
|
load_file_into_database(sys.argv[1], connection)
|
||||||
|
|
||||||
# Now, let's query
|
# Now, let's query
|
||||||
c = connection.cursor()
|
with sqlite3.connect('tf.db') as connection:
|
||||||
c.execute("SELECT value, COUNT(*) as C FROM words GROUP BY value ORDER BY C DESC")
|
c = connection.cursor()
|
||||||
for i in range(25):
|
c.execute("SELECT value, COUNT(*) as C FROM words GROUP BY value ORDER BY C DESC")
|
||||||
row = c.fetchone()
|
for i in range(25):
|
||||||
if row != None:
|
row = c.fetchone()
|
||||||
print row[0] + ' - ' + str(row[1])
|
if row != None:
|
||||||
|
print row[0] + ' - ' + str(row[1])
|
||||||
connection.close()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user