Spaces:
Sleeping
Sleeping
Update aux_functions/db_functions.py
Browse files- aux_functions/db_functions.py +122 -122
aux_functions/db_functions.py
CHANGED
@@ -1,123 +1,123 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import csv
|
3 |
-
import os
|
4 |
-
def read_query_from_file(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
5 |
-
file_path = 'aux_data/query.txt'
|
6 |
-
file_name=os.path.join(proj_dir,file_path)
|
7 |
-
try:
|
8 |
-
with open(file_name, 'r') as file:
|
9 |
-
CreateTable = file.read()
|
10 |
-
return CreateTable
|
11 |
-
except FileNotFoundError:
|
12 |
-
print(f"File {file_path} not found.")
|
13 |
-
return None
|
14 |
-
|
15 |
-
def file_name_from_file(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
16 |
-
file_path = 'aux_data/file_name.txt'
|
17 |
-
file_name=os.path.join(proj_dir,file_path)
|
18 |
-
try:
|
19 |
-
with open(file_name, 'r') as file:
|
20 |
-
return file.read().strip() # Remove leading/trailing whitespaces
|
21 |
-
except FileNotFoundError:
|
22 |
-
print(f"File {file_path} not found.")
|
23 |
-
return None
|
24 |
-
|
25 |
-
|
26 |
-
import sqlite3
|
27 |
-
|
28 |
-
def drop_sqlite_database():
|
29 |
-
"""
|
30 |
-
Deletes the SQLite database file at the specified path.
|
31 |
-
|
32 |
-
Parameters:
|
33 |
-
db_path (str): Path to the SQLite database file.
|
34 |
-
|
35 |
-
Returns:
|
36 |
-
None
|
37 |
-
"""
|
38 |
-
|
39 |
-
db_path = 'db.db'
|
40 |
-
try:
|
41 |
-
if os.path.exists(db_path):
|
42 |
-
os.remove(db_path)
|
43 |
-
print(f"Database at {db_path} has been deleted.")
|
44 |
-
else:
|
45 |
-
print(f"No database file found at {db_path}.")
|
46 |
-
except Exception as e:
|
47 |
-
print(f"An error occurred while deleting the database: {e}")
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
def setup_database(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test")
|
55 |
-
|
56 |
-
import sqlite3
|
57 |
-
create_table_query=read_query_from_file(proj_dir)
|
58 |
-
# create_table_query_2 =create_table_querry
|
59 |
-
|
60 |
-
# print(create_table_query_2)
|
61 |
-
connection = sqlite3.connect('db.db')
|
62 |
-
cursor = connection.cursor()
|
63 |
-
|
64 |
-
|
65 |
-
cursor.execute(str(create_table_query))
|
66 |
-
connection.commit()
|
67 |
-
connection.close()
|
68 |
-
|
69 |
-
|
70 |
-
import sqlite3
|
71 |
-
import csv
|
72 |
-
|
73 |
-
def import_csv(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
74 |
-
try:
|
75 |
-
connection = sqlite3.connect('db.db')
|
76 |
-
c = connection.cursor()
|
77 |
-
|
78 |
-
file = file_name_from_file(proj_dir) # Assuming this function is defined elsewhere
|
79 |
-
print("File to be imported:", file)
|
80 |
-
folder_name=os.path.join(proj_dir,"data")
|
81 |
-
|
82 |
-
with open(f"{folder_name}/{file}.csv", 'r', encoding='utf-8') as f:
|
83 |
-
reader = csv.reader(f)
|
84 |
-
print("Opened CSV file")
|
85 |
-
next(reader) # Skip the header row if present
|
86 |
-
|
87 |
-
# Dynamically determine the number of columns in the CSV
|
88 |
-
first_row = next(reader)
|
89 |
-
num_columns = len(first_row)
|
90 |
-
placeholders = ','.join(['?'] * num_columns)
|
91 |
-
|
92 |
-
# Re-insert the first row into the reader
|
93 |
-
reader = csv.reader(f)
|
94 |
-
next(reader) # Skip the header row again
|
95 |
-
|
96 |
-
for row in reader:
|
97 |
-
print("Inserting row:", row)
|
98 |
-
c.execute(f"INSERT INTO {file} VALUES ({placeholders})", row)
|
99 |
-
|
100 |
-
connection.commit()
|
101 |
-
print("Data imported successfully")
|
102 |
-
except sqlite3.Error as e:
|
103 |
-
print("SQLite error:", e)
|
104 |
-
except Exception as e:
|
105 |
-
print("Error:", e)
|
106 |
-
finally:
|
107 |
-
if connection:
|
108 |
-
connection.close()
|
109 |
-
print("Database connection closed")
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
if __name__ == "__main__":
|
116 |
-
print("h1")
|
117 |
-
print(file_name_from_file())
|
118 |
-
print("h2")
|
119 |
-
drop_sqlite_database()
|
120 |
-
print("h3")
|
121 |
-
setup_database()
|
122 |
-
print("h4")
|
123 |
import_csv()
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import csv
|
3 |
+
import os
|
4 |
+
def read_query_from_file(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
5 |
+
file_path = 'aux_data/query.txt'
|
6 |
+
file_name=os.path.join(proj_dir,file_path)
|
7 |
+
try:
|
8 |
+
with open(file_name, 'r') as file:
|
9 |
+
CreateTable = file.read()
|
10 |
+
return CreateTable
|
11 |
+
except FileNotFoundError:
|
12 |
+
print(f"File {file_path} not found.")
|
13 |
+
return None
|
14 |
+
|
15 |
+
def file_name_from_file(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
16 |
+
file_path = 'aux_data/file_name.txt'
|
17 |
+
file_name=os.path.join(proj_dir,file_path)
|
18 |
+
try:
|
19 |
+
with open(file_name, 'r') as file:
|
20 |
+
return file.read().strip() # Remove leading/trailing whitespaces
|
21 |
+
except FileNotFoundError:
|
22 |
+
print(f"File {file_path} not found.")
|
23 |
+
return None
|
24 |
+
|
25 |
+
|
26 |
+
import sqlite3
|
27 |
+
|
28 |
+
def drop_sqlite_database():
|
29 |
+
"""
|
30 |
+
Deletes the SQLite database file at the specified path.
|
31 |
+
|
32 |
+
Parameters:
|
33 |
+
db_path (str): Path to the SQLite database file.
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
None
|
37 |
+
"""
|
38 |
+
|
39 |
+
db_path = 'db.db'
|
40 |
+
try:
|
41 |
+
if os.path.exists(db_path):
|
42 |
+
os.remove(db_path)
|
43 |
+
print(f"Database at {db_path} has been deleted.")
|
44 |
+
else:
|
45 |
+
print(f"No database file found at {db_path}.")
|
46 |
+
except Exception as e:
|
47 |
+
print(f"An error occurred while deleting the database: {e}")
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
def setup_database(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
55 |
+
|
56 |
+
import sqlite3
|
57 |
+
create_table_query=read_query_from_file(proj_dir)
|
58 |
+
# create_table_query_2 =create_table_querry
|
59 |
+
|
60 |
+
# print(create_table_query_2)
|
61 |
+
connection = sqlite3.connect('db.db')
|
62 |
+
cursor = connection.cursor()
|
63 |
+
|
64 |
+
|
65 |
+
cursor.execute(str(create_table_query))
|
66 |
+
connection.commit()
|
67 |
+
connection.close()
|
68 |
+
|
69 |
+
|
70 |
+
import sqlite3
|
71 |
+
import csv
|
72 |
+
|
73 |
+
def import_csv(proj_dir=r"C:\Users\pcraj\OneDrive\Desktop\projects\new_test"):
|
74 |
+
try:
|
75 |
+
connection = sqlite3.connect('db.db')
|
76 |
+
c = connection.cursor()
|
77 |
+
|
78 |
+
file = file_name_from_file(proj_dir) # Assuming this function is defined elsewhere
|
79 |
+
print("File to be imported:", file)
|
80 |
+
folder_name=os.path.join(proj_dir,"data")
|
81 |
+
|
82 |
+
with open(f"{folder_name}/{file}.csv", 'r', encoding='utf-8') as f:
|
83 |
+
reader = csv.reader(f)
|
84 |
+
print("Opened CSV file")
|
85 |
+
next(reader) # Skip the header row if present
|
86 |
+
|
87 |
+
# Dynamically determine the number of columns in the CSV
|
88 |
+
first_row = next(reader)
|
89 |
+
num_columns = len(first_row)
|
90 |
+
placeholders = ','.join(['?'] * num_columns)
|
91 |
+
|
92 |
+
# Re-insert the first row into the reader
|
93 |
+
reader = csv.reader(f)
|
94 |
+
next(reader) # Skip the header row again
|
95 |
+
|
96 |
+
for row in reader:
|
97 |
+
print("Inserting row:", row)
|
98 |
+
c.execute(f"INSERT INTO {file} VALUES ({placeholders})", row)
|
99 |
+
|
100 |
+
connection.commit()
|
101 |
+
print("Data imported successfully")
|
102 |
+
except sqlite3.Error as e:
|
103 |
+
print("SQLite error:", e)
|
104 |
+
except Exception as e:
|
105 |
+
print("Error:", e)
|
106 |
+
finally:
|
107 |
+
if connection:
|
108 |
+
connection.close()
|
109 |
+
print("Database connection closed")
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
print("h1")
|
117 |
+
print(file_name_from_file())
|
118 |
+
print("h2")
|
119 |
+
drop_sqlite_database()
|
120 |
+
print("h3")
|
121 |
+
setup_database()
|
122 |
+
print("h4")
|
123 |
import_csv()
|