-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathspider_schema_to_sqlite.py
58 lines (52 loc) · 2.08 KB
/
spider_schema_to_sqlite.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
"""
Usage: python3 tools/spider_schema_to_sqlite.py
Input: Reads data/spider-schema.csv
Output: Creates databases/*.sqllite files.
Author: prasad
"""
import os
import csv
import sqlite3
with open("data/spider-schema.csv") as f:
databases = {}
csvfile = csv.reader(f, skipinitialspace=True)
header = None
for line in csvfile:
if header is None:
header = line
continue
row = dict(zip(header, line))
db = row["Database name"].lower()
table = row["Table Name"].lower()
column = row["Field Name"].lower()
column_type = row["Type"]
column_primray = row["Is Primary Key"]
if db not in databases:
databases[db] = {"tables": {}}
if table not in databases[db]["tables"]:
databases[db]["tables"][table] = {"columns": {}, "primary": []}
if column not in databases[db]["tables"][table]["columns"]:
databases[db]["tables"][table]["columns"][column] = { "name": column, "type": column_type }
if column_primray == "True":
databases[db]["tables"][table]["primary"].append(column)
for db in databases:
os.makedirs("databases/" + db)
dbconn = sqlite3.connect("databases/" + db + "/" + db + ".sqlite")
dbcur = dbconn.cursor()
for table in databases[db]["tables"]:
if "sqlite_sequence" == table:
continue
tablesql = "CREATE TABLE " + table + "("
coldelim = " "
for col in databases[db]["tables"][table]["columns"]:
col = databases[db]["tables"][table]["columns"][col]
tablesql += coldelim + '"' + col["name"] + '" ' + col["type"]
coldelim = ","
if len(databases[db]["tables"][table]["primary"]):
tablesql += ",PRIMARY KEY ("+ ",".join(databases[db]["tables"][table]["primary"]) +")"
tablesql += ");"
print (tablesql)
dbcur.execute(tablesql)
dbconn.commit()
f.close()
print("\nYou can now use databases/*.sqlite files\n")