-
Notifications
You must be signed in to change notification settings - Fork 4
/
sql_db.py
71 lines (48 loc) · 1.75 KB
/
sql_db.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
import sqlite3
def create_table(connection: sqlite3.Connection) -> None:
cursor = connection.cursor()
sql_command = """
CREATE TABLE repositories (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(127) NOT NULL,
author VARCHAR(127) NOT NULL,
fork_url VARCHAR(127) NOT NULL,
orig_url VARCHAR(127) NOT NULL
);
"""
cursor.execute(sql_command)
connection.commit()
def add_new_entry(connection: sqlite3.Connection, name: str, author: str, fork_url: str, orig_url: str) -> None:
cursor = connection.cursor()
sql_command = f"""
SELECT id, name, author FROM repositories
WHERE orig_url = "{orig_url}";
"""
cursor.execute(sql_command)
results = cursor.fetchall()
if len(results) > 0:
print(f"Repository for URL {orig_url} already exists, not adding it to database!")
return
sql_command = f"""
INSERT INTO repositories (name, author, fork_url, orig_url)
VALUES ("{name}", "{author}", "{fork_url}", "{orig_url}");
"""
cursor.execute(sql_command)
connection.commit()
if __name__ == "__main__":
connection = sqlite3.connect("repos.db")
# create_table(connection)
# Add new entry
name = "CadenceLibrary"
author = "XiaoSenLuo"
fork_url = "https://github.com/Werni2A/XiaoSenLuo_CadenceLibrary"
orig_url = "https://github.com/XiaoSenLuo/CadenceLibrary"
add_new_entry(connection, name, author, fork_url, orig_url)
cursor = connection.cursor()
# Print all entries
cursor.execute("SELECT * FROM repositories")
repos = cursor.fetchall()
for repo in repos:
print(repo)
connection.commit()
connection.close()