-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_query.py
108 lines (83 loc) · 3.22 KB
/
sql_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
# -*- encoding: utf-8-*-
import sqlite3
import csv
from pprint import pprint
import os
#queries used for database
#will need to change db if an
query_key = 'SELECT key, count(*) as num FROM nodes_tags GROUP BY key ORDER BY num DESC'
query_amenity = '''SELECT value, count(*) num FROM nodes_tags
WHERE key = "amenity"
GROUP BY value
HAVING num >= 25
ORDER BY num DESC
'''
query_food = '''SELECT * FROM nodes_tags
WHERE key = "amenity" AND (value ="restaurant" OR value = "fast_food")
'''
query_food_count = '''SELECT count(*) as num FROM nodes_tags
WHERE (value ="restaurant" OR value = "fast_food")
'''
#is this bad syntax below im using a where clause instead of and.
query_food_names = '''SELECT a.value, b.value
FROM nodes_tags b, nodes_tags a
WHERE b.id = a.id and (a.value = "restaurant" OR a.value = "fast_food") and b.key = "name"
'''
query_burger_american = '''SELECT a.id, a.value, b.value
FROM nodes_tags b, nodes_tags a
WHERE b.id = a.id and (a.value = 'burger' or a.value = 'american') and b.key = "name"
'''
query_cafe_coffee = '''SELECT a.id, a.value, b.value
FROM nodes_tags b, nodes_tags a
WHERE b.id = a.id and (a.value = 'cafe' or a.value ='coffee_shop') and b.key = "name"
ORDER by b.value
'''
query_cuisine = '''SELECT value, count(*) as count
FROM nodes_tags
WHERE key = 'cuisine'
GROUP BY value
ORDER by count DESC
'''
query_users = '''SELECT c.user, count(*) as num
FROM (SELECT user FROM nodes UNION ALL SELECT user FROM ways) c
GROUP BY c.user
ORDER BY num DESC
'''
query_unique= '''SELECT count(DISTINCT(c.user)) as num
FROM (SELECT user FROM nodes UNION ALL SELECT user FROM ways) c
'''
#HAVING num > 1000
query_postcode = '''SELECT zip.value, count(*) as count
FROM
(SELECT value, key FROM nodes_tags
WHERE key = 'postcode' UNION ALL
SELECT value, key FROM ways_tags
WHERE key = 'postcode') zip
GROUP BY zip.value
ORDER by count DESC
'''
query_city = '''SELECT city.value, count(*) as count
FROM
(SELECT value, key FROM nodes_tags
WHERE key = 'city' UNION ALL
SELECT value, key FROM ways_tags
WHERE key = 'city') city
GROUP BY city.value
ORdER BY count DESC
'''
query_clean_street = 'SELECT key, value FROM nodes_tags WHERE (key = "street" and value LIKE "%St%")'
db = os.path.join(os.getcwd(), "sqlite_windows/madison.db")
con = sqlite3.connect(db)
cur = con.cursor()
#cur.execute('DROP TABLE IF EXISTS nodes_tags')
#conn.commit()
'''
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
print(cursor.fetchall())
'''
#get_file_size(files)
cur.execute(query_food_count)
answer = cur.fetchall()
pprint(answer)
con.close()