-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulate_database.py
37 lines (32 loc) · 1002 Bytes
/
populate_database.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
import psycopg2
import psycopg2.extras
import os
def insert_into_bulk_database(cursor, data):
try:
insert_query = """
INSERT INTO wikipedia_data (id, title, content, parsed_content)
VALUES %s;
"""
psycopg2.extras.execute_values(
cursor, insert_query, data, page_size=100
)
except Exception as error:
print(f"Error: {error}")
def insert_into_database(wikiid, article_title, article_content):
try:
connection = psycopg2.connect(
database="wikivi"
)
cursor = connection.cursor()
insert_query = """
INSERT INTO wikipedia_data (id, title, content)
VALUES (%s, %s, %s);
"""
cursor.execute(insert_query, (wikiid, article_title, article_content))
connection.commit()
except Exception as error:
print(f"Error: {error}")
finally:
if connection:
cursor.close()
connection.close()