-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertStationInfoInDatabase.py
More file actions
115 lines (83 loc) · 3.61 KB
/
insertStationInfoInDatabase.py
File metadata and controls
115 lines (83 loc) · 3.61 KB
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
109
110
111
112
113
114
# This function inserts station information from a CSV file into a PostgreSQL database.
# It uses the provided input file to read station data and inserts it into the 'station_info' table in the database.
# **Importing data files in a Python program**
# ---------------------------------------------
#
#
# from japanAirAnalytics.store import stationInfo as db
#
# obj = stationInfo()
#
# obj.insert(inputFile)
import csv
import sys
import psycopg2
from alive_progress import alive_bar
# SQL query to create stationInformation table
# query = CREATE TABLE stationInfo(sid int not null, geog geography(POINT,4326), addressInfo varchar)
class stationInfo:
"""
:Description: This class inserts station information from a CSV file into a PostgreSQL database. It reads station data from the provided input file and inserts it into the 'station_info' table in the database.
:param inputFile: str :
File containing station information (-stationAdd.txt file in Data folder).
:Methods:
insert(inputFile): This method reads a CSV file and inserts station information into the 'station_info' table.
**Executing on Python Terminal**
----------------------------------
Format:
>>> stationInfo.insert(inputFile)
Example:
>>> stationInfo.insert("station_info_data.csv")
**Importing data files in a Python program**
---------------------------------------------
from japanAirAnalytics.store import stationInfo as db
obj = stationInfo()
obj.insert(inputFile)
"""
def insert(inputFile):
"""
This method reads the specified CSV file and inserts station information into the 'station_info' table in a PostgreSQL database.
:param: inputFile: str :
The path to the CSV file containing station information. Example: stationInfo.insert("station_info_data.csv")
Returns:
None
"""
conn = None
try:
conn = psycopg2.connect(database="soramame", user="temp", password="BunnyBittu@143", host="163.143.165.136", port=5432)
# create a cursor
cur = conn.cursor()
lines = 0;
with open(inputFile, 'r') as fp:
lines = len(fp.readlines())
# Open the CSV file
csv_file = open(inputFile, encoding="utf-8", errors="",
newline="")
fileObject = csv.reader(csv_file, delimiter=",")
print("Inserting the information on stations into the database")
with alive_bar(lines) as bar:
for row in fileObject:
bar()
query = "insert into station_info values(\'" + row[0] + '\',\'' + row[1] + '\',\'' + row[2] + '\',\'' + row[3] + "\')"
print(query)
# executes query
cur.execute(query)
conn.commit()
cur.close()
# Exception handling
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
# close database connection
conn.close()
print('Database connection closed.')
if __name__ == '__main__':
"""
Start the main() Method
"""
if len(sys.argv) < 2:
print("Error : Incorrect number of input parameters")
print("Format: python3 stationInfo.py fileName")
else:
stationInfo.insert(sys.argv[1])