-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbConnector.py
37 lines (31 loc) · 1.29 KB
/
DbConnector.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
from pymongo import MongoClient, version
class DbConnector:
"""
Connects to the MongoDB server on the Ubuntu virtual machine.
Connector needs HOST, USER and PASSWORD to connect.
Example:
HOST = "tdt4225-00.idi.ntnu.no" // Your server IP address/domain name
USER = "testuser" // This is the user you created and added privileges for
PASSWORD = "test123" // The password you set for said user
"""
def __init__(self,
DATABASE='db_task3',
HOST="tdt4225-38.idi.ntnu.no",
USER="Gr38",
PASSWORD="123"):
uri = "mongodb://%s:%s@%s/%s" % (USER, PASSWORD, HOST, DATABASE)
# Connect to the databases
try:
self.client = MongoClient(uri)
self.db = self.client[DATABASE]
except Exception as e:
print("ERROR: Failed to connect to db:", e)
# get database information
print("You are connected to the database:", self.db.name)
print("-----------------------------------------------\n")
def close_connection(self):
# close the cursor
# close the DB connection
self.client.close()
print("\n-----------------------------------------------")
print("Connection to %s-db is closed" % self.db.name)